Description:
Obtain a cursor object containing the specified data and the activity takes over the life cycle of the cursor.
First, this function obtains a cursor object containing the specified data (specified by the query method parameter) by calling getcontentresolver (). Query (Uri, projection, selection, selectionargs, sortorder.
Call startmanagingcursor (c) to take over the life cycle of the returned cursor C.
Prototype:
Public FinalCursor managedquery (URI Uri,
String [] projection,
String selection,
String [] selectionargs,
String sortorder)
{
Cursor c = getcontentresolver (). Query (Uri, projection, selection, selectionargs, sortorder );
If(C! = NULL ){
Startmanagingcursor (C );
}
ReturnC;
}
Parameters:
Uri,Uri used for content provider query, that is, to obtain data from this URI. For example:
Uri uri = contacts. People. content_uri;// Contact list Uri.
ProjectionTo identify which columns in the URI need to be included in the returned cursor object. For example:
// Columns to be queried
String [] projection = {contacts. peoplecolumns. Name, contacts. peoplecolumns. Notes };
SelectionAs the filter parameter for the query (filtering out data that meets the selection), similar to the condition selection after the where statement in SQL. For example:
String selection = contacts. People. Name + "= ?"// Query Conditions
Selectionargs,The query condition parameter, used with the selection parameter. For example:
String [] selectionargs = {"braincol", "nixn. Dev "};// Query Condition Parameters
Sortorder,Sort the query results by a column in the projection parameter ). For example:
String sortorder = contacts. peoplecolumns. Name;// Sort the query result by the specified query Column)
Return Value:
A cursor object that contains the specified data.
Example:
Uri uri = contacts. People. content_uri;
String [] projection = {contacts. peoplecolumns. Name,
Contacts. peoplecolumns. Notes };
String selection = contacts. peoplecolumns. Name +"=? ";
String [] selectionargs = {"Braincol","Nixn. Dev"};
String sortorder = contacts. peoplecolumns. Name;
// Use managedquery to obtain the contacts. People contentprovider's cursor.
Cursor cursor = managedquery (Uri, projection, selection, selectionargs, sortorder );
The preceding example indicates that the name is braincol and nixn in the contact list. the "name" and "Notes" information of the two contacts in Dev are sorted by name, and the sorted results are packaged in a cursor object and returned.