SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

 qb.setTables(getTableName());

 if (isCollectionUri(url)) {

  qb.setProjectionMap(getDefaultProjection ());

 } else {

  qb.appendWhere(getIdColumnName ()+'='+url.getPathSegments().get(1));

 }

 String orderBy;

 if (TextUtils.isEmpty(sort)) {

  orderBy = getDefaultSortOrder();

 } else {

  orderBy = sort;

 }

 Cursor c = qb.query(db, projection, selection, selectionArgs,

  nullnull, orderBy);

 c.setNotificationUri(getContext ().getContentResolver(), url);

 return c;

}

We create a SQLiteQueryBuilder and pour the query details into the builder. Note that the query could be based on either a collection or an instance Uri — in the latter case, we need to add the instance ID to the query. When done, we use the query() method on the builder to get a Cursor for the results.

insert()

Your insert() method will receive a Uri representing the collection and a ContentValues structure with the initial data for the new instance. You are responsible for creating the new instance, filling in the supplied data, and returning a Uri to the new instance.

If this is a SQLite-backed content provider, once again, the implementation is mostly boilerplate: validate that all required values were supplied by the activity, merge your own notion of default values with the supplied data, and call insert() on the database to actually create the instance.

For example, here is insert() from Provider:

@Override

public Uri insert(Uri url, ContentValues initialValues) {

 long rowID;

 ContentValues values;

 if (initialValues!=null) {

  values = new ContentValues(initialValues);

 } else {

  values = new ContentValues();

 }

 if (!isCollectionUri(url)) {

  throw new IllegalArgumentException('Unknown URL ' + url);

 }

 for (String colName : getRequiredColumns()) {

  if (values.containsKey(colName) == false) {

   throw new IllegalArgumentException('Missing column: ' + colName);

  }

 }

 populateDefaultValues(values);

 rowID = db.insert(getTableName (), getNullColumnHack(), values);

 if (rowID > 0) {

  Uri uri = ContentUris.withAppendedId(getContentUri(), rowID);

  getContext().getContentResolver ().notifyChange(uri, null);

  return uri;

 }

 throw new SQLException('Failed to insert row into ' + url);

}

The pattern is the same as before: use the provider particulars plus the data to be inserted to actually do the insertion. Please note the following:

• You can insert only into a collection Uri, so we validate that by calling isCollectionUri().

• The provider knows what columns are required (getRequiredColumns()), so we iterate over those and confirm our supplied values cover the requirements.

• The provider is responsible for filling in any default values (populateDefaultValues()) for columns not supplied in the insert() call and not automatically handled by the SQLite table definition.

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’

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

0

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

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