Querying for Content

Content Provider queries take a form very similar to that of database queries. Query results are returned as Cursors over a result set, like databases, in the same way as described previously in this chapter.

You can extract values from the result Cursor using the same techniques described within the database section on "Extracting Results from a Cursor.''

Using the query method on the ContentResolver object, pass in:

> The URI of the Content Provider data you want to query.

> A projection that lists the columns you want to include in the result set.

> A where clause that defines the rows to be returned. You can include ? wildcards that will be replaced by the values passed into the selection argument parameter.

> An array of selection argument strings that will replace the ?s in the where clause.

> A string that describes the order of the returned rows.

Listing 7-13 shows how to use a Content Resolver to apply a query to a Content Provider:

LISTING 7-13: Querying a Content Provider with a Content Resolver

ContentResolver cr = getContentResolver(); // Return all rows

Cursor allRows = cr.query(MyProvider.CONTENT_URI, null, null, null, null); // Return all columns for rows where column 3 equals a set value // and the rows are ordered by column 5. String where = KEY_COL3 + "=" + requiredValue; String order = KEY_COL5;

Cursor someRows = cr.query(MyProvider.CONTENT_URI, null, where, null, order);

You'll see more examples of querying for content later in this chapter when the native Android Content Providers are introduced.

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment