Querying a Content Provider

You need three pieces of information to query a content provider:

• The URI that identifies the provider

• The names of the data fields you want to receive

• The data types for those fields

If you're querying a particular record, you also need the ID for that record. Making the query

To query a content provider, you can use either the ContentResolver.query() method or the

Activity.managedQuery() method. Both methods take the same set of arguments, and both return a Cursor object. However, managedQuery() causes the activity to manage the life cycle of the Cursor. A managed Cursor handles all of the niceties, such as unloading itself when the activity pauses, and requerying itself when the activity restarts. You can ask an Activity to begin managing an unmanaged Cursor object for you by calling Activity.startManagingCursor() .

The first argument to either query () or managedQuery() is the provider URI — the CONTENT URI constant that identifies a particular ContentProvider and data set (see URIs earlier).

A query returns a Cursor object that can move from record to record and column to column to read the contents of each field. It has specialized methods for reading each type of data. So, to read a field, you must know what type of data the field contains. (There's more on query results and Cursor objects later.)

0 0

Post a comment