Книга: Beginning Android

update()

update()

Your update() method gets the Uri of the instance or collection to change, a ContentValues structure with the new values to apply, a String for a SQL WHERE clause, and a String[] with parameters to use to replace ? found in the WHERE clause. Your responsibility is to identify the instance(s) to be modified (based on the Uri and WHERE clause), then replace those instances’ current property values with the ones supplied.

This will be annoying unless you’re using SQLite for storage. Then you can pretty much pass all the parameters you received to the update() call to the database, though the update() call will vary slightly depending on whether you are updating one instance or several.

For example, here is update() from Provider:

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

In this case, updates can either be to a specific instance or applied across the entire collection, so we check the Uri (isCollectionUri()) and, if it is an update for the collection, just perform the update. If we are updating a single instance, we need to add a constraint to the WHERE clause to only update for the requested row.

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

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

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