Android API: activity. managedquery ()

Source: Internet
Author: User
Tags sorted by name

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.