Android Tutorial

(avery) #1
Android Tutorial 353

The Uri is first validated to make sure it’s one where inserting
makes sense. A Uri targeting a particular row would not, for
instance. Next, a writeable database object instance is retrieved.
Using this, the database insert() method is called on the table
defined by the incoming Uri and with the values passed in. At this
point, no error checking is performed on the values. Instead, the
underlying database implementation throws exceptions that can be
handled by the user of the content provider.

If the insert was successful, a Uri is created for notifying the
system of a change to the underlying data via a call to the
notifyChange() method of the Content Resolver. Otherwise, an
exception is thrown.

Implementing the update() Method

The update() method is used to modify an existing row of data. It
has elements similar to the insert() and query() methods. The
update is applied to a particular selection defined by the incoming
Uri.

public int update(Uri uri, ContentValues values,
String selection, String[] selectionArgs) {
SQLiteDatabase sqlDB = mDB.getWritableDatabase();
int match = sURIMatcher.match(uri);
int rowsAffected;
switch (match) {
case TRACKPOINTS:
rowsAffected = sqlDB.update(
TrackPointDatabase.TRACKPOINTS_TABLE,
values, selection, selectionArgs);
break;
case TRACKPOINT_ID:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {

Free download pdf