Android Tutorial

(avery) #1
Android Tutorial 355

the user might be deleting more data than they should. You also
notice that this is similar to the update() method.

public int delete(Uri uri, String selection, String[] selectionArgs)
{
int match = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = mDB.getWritableDatabase();
int rowsAffected = 0;
switch (match) {
case TRACKPOINTS:
rowsAffected = sqlDB.delete(
TrackPointDatabase.TRACKPOINTS_TABLE,
selection, selectionArgs);
break;
case TRACKPOINT_ID:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsAffected =
sqlDB.delete(TrackPointDatabase.TRACKPOINTS_TABLE,
_ID+”=”+id, null);
} else {
rowsAffected =
sqlDB.delete(TrackPointDatabase.TRACKPOINTS_TABLE,
selection + “ and “ +_ID+”=”+id, selectionArgs);
}
break;
default:
throw new IllegalArgumentException(
“Unknown or Invalid URI “ + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsAffected;
}


Again, a writable database instance is retrieved and the Uri type is
determined using the match method of UriMatcher. If the result is a
directory Uri, the delete is called with the selection the user passed
in. However, if the result is a specific row, the row index is used to
further limit the delete, with or without the selection. Allowing this
Free download pdf