Apidemos-->views-lists-cursor (people)-mainly used to get mobile contact information, belonging to the content provider category, to understand this aspect of the content, you can refer to the official Docs/sdk/docs/guide/topics /providers/content-provider-basics.html
Here's a brief.ContentProvider;ContentProvider enables data sharing between different applications.It is the API for data exchange between programs.When a program needs to expose its own data to other programs for use, the application can be implemented by providing ContentProvider, and other applications can contentprovider exposed data by Contentresolver operations.
The system-provided contacts are used in the instance ContentProvider: Several commonly used URIs are: ContactsContract.Contacts.CONTENT_URL//Manage the URI of the contactcontactscontract.contacts._id//Contact ID
ContactsContract.Contacts.DISPLAY_NAME//The URI of the contact NAME
ContactsContract.CommonDataKinds.Phone.CONTENT_URI The URI of the phone that manages the contactContactsContract.CommonDataKinds.Email.CONTENT_URI Manage Contact's e-mail URI
Just write it down and make a super-low mistake. Actually used for to traverse the cursor, lost the movetonext. There is also the use of closing cursors, plusmcursor.close ();
Package Com.example.testmyviewslistscursorpeople;import Java.util.arraylist;import Java.util.list;import Android.app.listactivity;import Android.content.context;import Android.database.cursor;import Android.net.localsocketaddress.namespace;import Android.os.bundle;import Android.provider.contactscontract.contacts;import Android.provider.ContactsContract.CommonDataKinds.Phone; Import Android.text.getchars;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.abslistview;import Android.widget.baseadapter;import Android.widget.listview;import Android.widget.textview;import com.example.testmyviewslistsactivateitems.r;/** * * @ Author Administrator imitation effect Slow loading Apidemos--Views-lists-slow Adapter */public class Main extends Listactivity {pri vate Boolean mbusy = false;public static list<string> mstrings = new arraylist<string> (); @Overrideprotected V OID onCreate (Bundle savedinstancestate) {super.oncreate (savedinstanceState), GetPeople (); Setlistadapter (new Slowadapter (this));//Set selection mode to Radio Getlistview (). Setchoicemode (Listview.choice _mode_single);//First load settings check Itemsgetlistview (). setitemchecked (0, true); Getlistview (). Setonscrolllistener (New Onscrolllistener ());} Protected class Onscrolllistener implements Listview.onscrolllistener {@Overridepublic void onscrollstatechanged ( Abslistview view, int scrollstate) {switch (scrollstate) {//The view is not scrolling.case Onscrolllistener.scroll_state_ Idle:mbusy = False;int first = View.getfirstvisibleposition (); int count = View.getchildcount (); for (int i = 0; i < Coun T i++) {TextView t = (TextView) view.getchildat (i); if (t.gettag () = null) {T.settext (Mstrings.get (first + i)); T.settag (nul l);}} break;//the user is scrolling using touch, and their finger is still on//the Screencase onscrolllistener.scroll_state_to Uch_scroll:mbusy = true;break;//The user had previously been scrolling using touch and had//performed a fling.//the ANI Mation is now coasting to a STOPcase OnScrollListener.SCROLL_STATE_FLING:mBusy = True;break;}} @Overridepublic void Onscroll (abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) {}}@ overrideprotected void Onlistitemclick (ListView l, View v, int position, long id) {Getlistview (). setitemchecked (position , true);} Custom adapter Private class Slowadapter extends Baseadapter {private layoutinflater minflater;public slowadapter (Context Context) {Minflater = (layoutinflater) context.getsystemservice (context.layout_inflater_service);} @Overridepublic int GetCount () {return mstrings.size ();} @Overridepublic Object getItem (int position) {return position;} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {TextView text;if (Convertview = = null) {TE XT = (TextView) minflater.inflate (R.layout.main, NULL, FALSE);} else {text = (TextView) Convertview;} if (!mbusy) {Text.settext (Mstrings.get (position). ToString ()); Text.settag (null);} else {text.settext ("Loading ..."); Text.settag (this);} return text;}} protected void GetPeople () {final string[] contact_projection = new string[] {contacts._id,contacts.display_name,}; Cursor mcursor = Getcontentresolver (). query (contacts.content_uri,contact_projection, NULL, NULL, NULL); while (Mcursor.movetonext ()) {String name = mcursor.getstring (Mcursor.getcolumnindex (contacts.display_name)); Mstrings.add (name); } mcursor.close ();} }
Example Source
Added ways to get phone contact phone calls (only for the understanding of Get methods, not yet correspond to contact one by one, subsequent improvements):
protected void GetPeople () {final string[] contact_projection = new string[] {contacts._id,contacts.display_name};//contacts Cursor mcursor = getcontentresolver (). query (contacts.content_uri,contact_projection, NULL, NULL, NULL);//Contact phone cursor Phones = Getcontentresolver (). query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL, ContactsContract.CommonDataKinds.Phone.CONTACT_ID, NULL, NULL); while (Mcursor.movetonext ()) {//Get contact String name = Mcursor.getstring (Mcursor.getcolumnindex (Contacts.display_name)); Mstrings.add (NAME); if (Phones.movetonext ()) {// Get phone string phone = phones.getstring (Phones.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); Mstrings.add (phone);}} Close Resource Mcursor.close ();p hones.close ();}