Summary of how to get phone address book information

Source: Internet
Author: User

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

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.