Книга: Beginning Android

delete()

delete()

Like update(), delete() receives a Uri representing the instance or collection to work with and a WHERE clause and parameters. If the activity is deleting a single instance, the Uri should represent that instance and the WHERE clause may be null. But the activity might be requesting to delete an open-ended set of instances, using the WHERE clause to constrain which ones to delete.

As with update(), though, this is simple if you are using SQLite for database storage (sense a theme?). You can let it handle the idiosyncrasies of parsing and applying the WHERE clause — all you have to do is call delete() on the database.

For example, here is delete() from Provider:

@Override
public int delete(Uri url, String where, String[] whereArgs) {
 int count;
 long rowId = 0;
 if (isCollectionUri(url)) {
  count = db.delete(getTableName(), where, whereArgs);
 } else {
  String segment = url.getPathSegments().get(1);
  rowId = Long.parseLong(segment);
  count = db.delete(getTableName(), getIdColumnName() + "="
   + segment + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""),
   whereArgs);
 }
 getContext().getContentResolver().notifyChange(url, null);
 return count;
}

This is almost a clone of the update() implementation described earlier in this chapter — either delete a subset of the entire collection or delete a single instance (if it also satisfies the supplied WHERE clause).

Оглавление книги

Оглавление статьи/книги

Генерация: 0.308. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз