Tag:android Database paging query system contact    
Package Com.example.yqqmobilesafe.contactprovider;import Java.util.arraylist;import java.util.list;import android. R.integer;import Android.content.context;import Android.database.cursor;import Android.net.Uri;import Android.provider.contactscontract;import Com.example.yqqmobilesafe.domain.contactinfo;public Class Contactinfoprovider {Private Context Mcontext;public Contactinfoprovider (context context) {Mcontext=context;} /** * Get System Contact information * @return */public list<contactinfo> getsystemcontactinfos () {list<contactinfo> infos=new Arraylist<contactinfo> ();//use Contentresolver to find contact data cursor cursor = mcontext.getcontentresolver (). Query ( ContactsContract.Contacts.CONTENT_URI, NULL, null,null, NULL);//Iterate through the results of the query, get all the contacts in the system while (Cursor.movetonext ()) { ContactInfo info=new ContactInfo ();//Get contact idstring contactId = cursor.getstring (Cursor.getcolumnindex ( contactscontract.contacts._id));//Gets the name of the contact string name = Cursor.getstring (Cursor.getcolumnindex ( ContactsContract.Contacts.DISPLAY_NAME)); Info.setcontactname (name);//Use Contentresolver to find the phone number of the contact cursor phones = mcontext.getcontentresolver (). Query ( Contactscontract.commondatakinds.phone.content_uri,null,contactscontract.commondatakinds.phone.contact_id+ "=" + ContactId, NULL, NULL);//Iterate through the results of the query, get multiple phone numbers for that contact while (Phones.movetonext ()) {//Get the data in the phone number column in the query results. String PhoneNumber = phones.getstring (Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); Info.setphonenumber (PhoneNumber);} Phones.close (); Infos.add (info); info=null;} Cursor.close (); return infos;} /** * Paging Query system Contact information * @param pageSize Maximum number per page * @param currentoffset Current offset * @return */public list<contactinfo> Getco Ntactsbypage (int pagesize,int currentoffset) {list<contactinfo> infos=new arraylist<contactinfo> (); Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; string[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.DATA1, "Sort_key"}; cursor cursor = Mcontext.getcontentresolVer (). Query (URI, projection, null, NULL, "Sort_key COLLATE localized ASC limit" + pageSize + "offset" + currentoffset); if (cursor! = NULL) {while (Cursor.movetonext ()) {ContactInfo info=new contactinfo (); String contactName = cursor.getstring (0); String PhoneNumber = cursor.getstring (1); Info.setcontactname (contactName); Info.setphonenumber (PhoneNumber); Infos.add (info); info=null;} Cursor.close ();} return infos;} /** * Total number of records for system contacts * @return */public int getallcounts () {int num=0;//use Contentresolver to find contact data cursor cursor = MCONTEXT.G Etcontentresolver (). query (ContactsContract.Contacts.CONTENT_URI, NULL, null,null, NULL);//traverse the query results to get all the contacts in the system while ( Cursor.movetonext ()) {num++;} Cursor.close (); return num;}}
Android Paging query get system contact information