Adding Updating and Deleting Content

To perform transactions on Content Providers, use the delete, update, and insert methods on the ContentResolver object.

Inserts

The Content Resolver offers two methods for inserting new records into your Content Provider — insert and bulkinsert. Both methods accept the URI of the item-type you're adding; where the former takes a single new ContentValues object, the latter takes an array.

Available for download on Wrax.com

The simple insert method will return a URI to the newly added record, while bulkinsert returns the number of successfully added rows.

Listing 7-14 shows how to use the insert and bulkinsert methods.

LISTING 7-14: Inserting new rows into a Content Provider

Available for download on Wrox.com

// Get the Content Resolver ContentResolver cr = getContentResolver();

// Create a new row of values to insert. ContentValues newValues = new ContentValues();

// Assign values for each row. newValues.put(COLUMN_NAME, newValue); [ ... Repeat for each column ... ]

Uri myRowUri = cr.insert(MyProvider.CONTENT_URI, newValues);

// Create a new row of values to insert. ContentValues[] valueArray = new ContentValues[5];

// TODO: Create an array of new rows int count = cr.bulkinsert(MyProvider.CONTENT_URI, valueArray);

Deletes

To delete a single record, call delete on the Content Resolver, passing in the URI of the row you want to remove. Alternatively, you can specify a where clause to remove multiple rows. Both techniques are shown in Listing 7-15.

LISTING 7-15: Deleting records from a Content Provider

Available for download on Wrox.com

ContentResolver cr = getContentResolver();

// Remove a specific row. cr.delete(myRowUri, null, null); // Remove the first five rows. String where = "_id < 5";

cr.delete(MyProvider.CONTENT_URI, where, null);

Updates

Content Provider row updates are made with the Content Resolver update method. The update method takes the URI of the target Content Provider, a ContentValues object that maps column names to updated values, and a where clause that indicates which rows to update.

When the update is executed, every row matched by the where clause is updated using the specified Content Values, and the number of successful updates is returned as shown in Listing 7-16.

LISTING 7-16: Updating records in a Content Provider Available for download on // Create a new row of values to insert. Wrax.com ContentValues newValues = new ContentValues();

// Create a replacement map, specifying which columns you want to // update, and what values to assign to each of them. newValues.put(COLUMN_NAME, newValue);

// Apply to the first 5 rows. String where = "_id < 5";

getContentResolver().update(MyProvider.CONTENT_URI, newValues, where, null);

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Responses

Post a comment