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.

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).

getType()

The last method you need to implement is getType(). This takes a Uri and returns the MIME type associated with that Uri. The Uri could be a collection or an instance Uri; you need to determine which was provided and return the corresponding MIME type.

For example, here is getType() from Provider:

@Override

public String getType(Uri url) {

 if (isCollectionUri(url)) {

  return(getCollectionType());

 }

 return(getSingleType());

}

As you can see, most of the logic delegates to private getCollectionType() and getSingleType() methods:

private String getCollectionType() {

 return('vnd.android.cursor.dir/vnd.commonsware.constant');

}

private String getSingleType() {

 return('vnd.android.cursor.item/vnd.commonsware.constant');

}

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату