Android Tutorial

(avery) #1

By : Ketan Bhimani


340 

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.

ContentValues values = new ContentValues();
values.put(People.NOTES, “This is my boss”);
Uri updateUri = ContentUris.withAppendedId(People.CONTENT_URI,
rowId);
int rows = getContentResolver().update(updateUri, values, null,
null);
Log.d(debugTag, “Rows updated: “ + rows);


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:

int rows = getContentResolver().delete(People.CONTENT_URI, null,
null);
Log.d(debugTag, “Rows: “+ rows);

Free download pdf