Modifying Content Providers Data Android

Content providers are not only static sources of data. They can also be used to add, update, and delete data, if the content provider application has implemented this functionality. Your application must have the appropriate permissions (that is, WRITE_ CONTACTS as opposed to READ_ CONTACTS) to perform some of these actions.

Adding Records

Using the Contacts content provider, we can, for example, add a new record to the contacts database programmatically.

Just as we used the ContentValues class to insert records into an application’s SQLite database,we use it again here. The first action we take is to provide a name for the Contacts.People.NAME column. We need to create the contact with a name before we can assign information, such as phone numbers. Think of this as creating a row in a table that provides a one-to-many relationship to a phone number table.

Next, we insert the data in the database found at the Contacts.People.CONTENT_URI path. We use a call to getContentResolver() to retrieve the ContentResolver associated with our Activity. The return value is the Uri of our new contact. We need to use it for adding phone numbers to our new contact. We then reuse the ContentValues instance by clearing it and adding a Contacts.Phones.NUMBER and the Contacts.Phones.TYPE for it. Using the ContentResolver,we insert this data into the newly created Uri.

Updating Records

Inserting data isn’t the only change you can make. You can update one or more rows, as well. The following block of code shows how to update data within a content provider. In this case, we update a note field for a specific contact, using its unique identifier.

Again, we use an instance of the ContentValues object to map the data field we want to update with the data value—in this case, the note field. This replaces any current note stored in the NOTES field currently stored with the contact. We then create the Uri for the specific contact we are updating. A simple call to the update() method of the Content Resolver class completes our change. We can then confirm that only one row was updated.

Deleting Records

Now that you cluttered up your contacts application with sample user data, you might want to delete some of it. Deleting data is fairly straightforward.

Deleting All Records

The following code deletes all rows at the given URI, although you should execute operations like this with extreme care:

The delete() method deletes all rows at a given URI filtered by the selection parameters, which, in this case, includes all rows at the People.CONTENT_URI location; in other words, all contact entries.

Deleting Specific Records

Often you want to select specific rows to delete by adding the unique identifier index to the end of the URI or remove rows matching a particular pattern.

For example, the following deletion matches all contact records with the name Sample User, which we used when we created sample contacts previously in the chapter.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics