How-to use Advanced Content Providers Within Our Android Application Programming Environment

How-to use Advanced Content Providers Within Our Android Application Programming Environment
Page content

If you read my last article you’ll remember what we saw regarding content providers: How-to use basic content providers within our android application programming environment, we essentially got the idea that content providers manage the information that is stored in SQLite databases (most data is stored in this way, but content providers can manage stored data in all ways you want). Maybe you are used to working with databases, maybe not, so let’s talk a bit about it.

In database management, we can get the information from the tables (using Queries), we can add new information to these tables (for example, we want to add a new contact to our phone book), or we can edit and specify records inserted before (for example, one of our contacts changes their phone number, we would like to change it). Maybe we are storing this information in a SQLite database, but the fact is that content providers can manage any kind of stored information, but to make it more easy to follow, let’s picture that our data is table-based.

It’s important to have in mind that writing in stored records may need some kind of permission. This will be explained in a future article, At this time however let’s focus on how to work with records.

Adding records

Let’s work with an easy example: We want to add a new contact to our phone book. To work with contacts we have stored in the phonebook we have to deal with the “People” content provider. First of all, we need to create a ContentValues object.

ContentValues mValues = new ContentValues();

Then we add the contact name, for example, to the ContentValues, using the “put” method.

mValues.put(People.NAME, “Jose B. Cortés”);

In the first parameter we set the column key where the value is going to be stored, this value is set in the second parameter. It would be something like this: “Put the name ‘Jose B. Cortés’ in the column ‘NAME’ inside the table ‘People’

Once this is done, we have to “order” to create this row using the following line:

Uri mUri = getContentResolver().insert(People.CONTENT_URI, mValues);

This line provides us the URI to query and get a Cursor for the new record, to further modify the record.

Adding information

Once we have inserted a record in our database, we can add more information to it. In our example, we have only the name of the contact, now let’s add a mobile number to it.

I created an URI object (mUri) in the last line, to use it here.

Let’s create another URI, this time a URI to access the phones resource:

Uri mMobileUri = Uri.withAppendedPath(mUri, People.Phones.CONTENT_DIRECTORY);

We append to the new URI the contact URI using “.withAppendedPath

We can create other ContentValue objects or use the same ones we were using before. We are going to use the same as before (this way we optimize code), but before using it, we have to clear the “mValues” contents with:

mValues.clear();

In Android Contacts we can enter different kinds of telephone numbers, we have to set what kind of phone we are going to insert. Our intention is to set a “Mobile phone number”, so we set this with:

mValues.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE);

Now we put the mobile number.

mValues.put(People.Phones.NUMBER, “9876543210”);

And now we do the “insert” in the row with:

getContentResolver().insert( mMobileUri, mValues);

Editing records

Let’s suppose that we want to add more functionality to our “Contact Manager”, editing the contact is necessary.

To do this task, we need a specific URI: The contact URI, or the URI with a specific ID. In our case, we have the mUri, created in the first “insert”. This URI is “aiming” to the contact we have inserted. If we have not created this URI, we can create one using this:

Where 15 is the ID of the contact we want to edit.

Once we have this, let’s create other “ContentValues” this time called “mEditValues”

ContentValues mEditValues = new ContentValues();

and now, like we have done before:

mEditValues.put( People.Phones.NUMBER, “909023423”);

Where “ 909023423” is the new number.

Then we use the “update” method:

getcontentResolver().update( mUriID, mEditValues, null, null);

Deleting recods

If we want to delete a record, we just need to do the following:

getContentResolver().delete(mUriID, null, null);

Where myUriID is the URI (with the specific ID) of the record we want to delete.

Follow up

If you want to know when new articles are released, subscribe yourself to the Google Android RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev

This post is part of the series: How to- Develop Applications on Android: More Functionality

This time we will learn more advanced functionality to add to our Android Applications.

  1. Guide to Creating Android App Menus
  2. How to create Advanced Menus Via Our Google Android Applications
  3. Guide to Using Basic Content Providers in Android Development
  4. Dev Guide to Advanced Content Providers on Android
  5. How to interact with Android Events