1. The most concise method
Using classes
Android.provider.ContactsContract.CommonDataKinds.Phone;
The code is as follows:
<span style= "FONT-SIZE:18PX;" > Cursor c = getcontentresolver (). query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL, NULL, NULL, NULL); Startmanagingcursor (c); ListAdapter adapter=new Simplecursoradapter (this, Android. R.layout.simple_list_item_2, C, new String[]{phone.display_name,phone.number}, new int[]{android. R.id.text1,android. R.ID.TEXT2}); </span>
You can then use the ListView to display your name and phone number.
Attention:
1. If a person has two telephones, they are displayed separately.
2. If there are two identical people, they will be displayed separately and will not show a single person.
Operation Result:
2. General methods
using Classes
<span style= "FONT-SIZE:18PX;" >android.provider.contactscontract.contacts </span>
The code is as follows:
<span style= "FONT-SIZE:18PX;" >Map<String,String> contacts; List<map<string,string>> list=new arraylist<map<string,string>> (); int nameindex=-1; Contentresolver Cr=getcontentresolver (); Cursor cur=cr.query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, null,null); while (Cur.movetonext ()) {number= ""; Get the name Nameindex=cur.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME); Name=cur.getstring (Nameindex); Get the phone number String contactId = cur.getstring (cur. Getcolumnindex (contactscontract.contacts._id)); Gets the ID number of the contact, the database ID in SQLite, Cursor phone = cr.query (ContactsContract.CommonDataKinds.Phone.CONTENT_UR I, NULL, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, NULL, NULL); while (Phone.movetonext ()) {String Strphonenumber = phone.getstring (Phone.getcolumninde X (ContactsContract.CommonDataKinds.Phone.NUMBER)); Cell phone Number field contact may have more than one + + strphonenumber+ "\ n"; } contacts=new hashmap<string,string> (); Put the map contacts.put ("name", name); Contacts.put ("number", number); List.add (contacts); } cur.close (); </span>
Get the name and phone number, put it in the map, and then inclined the map into the list.
You can then use the ListView to display the name and phone number:
Some of the code is as follows:
<span style= "FONT-SIZE:18PX;" >simpleadapter adapter = new Simpleadapter (this,list,android. R.layout.simple_list_item_2, new string[]{"name", "number"}, new int[]{android. R.id.text1,android. R.ID.TEXT2}); Listview.setadapter (adapter); </span>
Attention:
1. If there are multiple phone numbers under a name, only one name and multiple numbers will be displayed.
2. If more than one name is present, multiple names are displayed.
Results:
Summary of how to get phone address book information