Modifying Data

Data kept by a content provider can be modified by:

• Adding new records

• Adding new values to existing records

• Batch updating existing records

• Deleting records the new record, and to further modify the record. Here's an example:

import android.provider.Contacts.People; import android.content.ContentResolver; import android.content.ContentValues; ContentValues values = new ContentValues();

// Add Abraham Lincoln to contacts and make him a favorite. values.put(People.NAME, "Abraham Lincoln"); // 1 = the new contact is added to favorites // 0 = the new contact is not added to favorites values.put(People.STARRED, 1);

Uri uri = getContentResolver().insert(People.CONTENT_URI, values); Adding new values

Once a record exists, you can add new information to it or modify existing information. For example, the next step in the example above would be to add contact information — like a phone number or an IM or e-mail address — to the new entry.

The best way to add to a record in the Contacts database is to append the name of the table where the new data goes to the URI for the record, then use the amended URI to add the new data values. Each Contacts table exposes a name for this purpose as a

CONTENT DIRECTORY constant. The following code continues the previous example by adding a phone number and e-mail address for the record just created:



= null;



= null;

All data modification is accomplished using ContentResolver methods. Some content providers require a more restrictive permission for writing data than they do for reading it. If you don't have permission to write to a content provider, the ContentResolver methods will fail.

0 0

Post a comment