Implementing the Query Method

The query method in a content provider is responsible for returning a collection of rows depending on an incoming URI and a where clause.

Like the other methods, the query method uses UriMatcher to identify the URI type. If the URI type is a single-item type, the method retrieves the book ID from the incoming URI like this:

1. It extracts the path segments using getPathSegments().

2. It indexes into the URI to get the first path segment, which happens to be the book ID.

The query method then uses the projections that we created in Listing 3-25 to identify the return columns. In the end, query returns the cursor to the caller. Throughout this process, the query method uses the SOLiteOueryBuilder object to formulate and execute the query (see Listing 3-27).

Listing 3-27. The queryO Method Implementation ^Override public Cursor query(Uri uri, String[] projection, String selection

, String[] selectionArgs, String sortOrder)

SOLiteOueryBuilder qb = new SOLiteOueryBuilder(); switch (sUriMatcher.match(uri))

0 0

Post a comment