Read contacts based on the first character of the contact name

Source: Internet
Author: User

1  Packagecom.zzw.contacts;2 3 Importjava.util.ArrayList;4 5 Importandroid.app.ListActivity;6 ImportAndroid.content.ContentResolver;7 ImportAndroid.content.Context;8 ImportAndroid.database.Cursor;9 ImportAndroid.net.Uri;Ten ImportAndroid.os.Bundle; One Importandroid.provider.ContactsContract; A ImportAndroid.view.LayoutInflater; - ImportAndroid.view.View; - ImportAndroid.view.ViewGroup; the ImportAndroid.widget.ArrayAdapter; - ImportAndroid.widget.ListView; - ImportAndroid.widget.TextView; -  +  Public classMainactivityextendslistactivity { -  + @Override A     protected voidonCreate (Bundle savedinstancestate) { at         Super. OnCreate (savedinstancestate); -         //Setcontentview (r.layout.activity_main); -  -arraylist<contact> contacts =NewArraylist<contact>(); - readcontacts (contacts); -  inListView ListView = This. Getlistview (); -  toarrayadapter<contact> adapter =NewMyadapter ( This, Android. R.layout.simple_list_item_2, contacts); + Listview.setadapter (adapter); -     } the  *     Private classMyadapterextendsArrayadapter<contact> { $ Panax Notoginseng         Private intresource; -         PrivateLayoutinflater Inflater; the         PrivateArraylist<contact>contacts; +  A          PublicMyadapter (Context context,intResource, arraylist<contact>contacts) { the             Super(context, resource, contacts); +              This. Resource =resource; -              This. Contacts =contacts; $Inflater =(Layoutinflater) Getsystemservice (context.layout_inflater_service); $         } -  - @Override the          Public intGetCount () { -             returncontacts.size ();Wuyi         } the  - @Override Wu          PublicContact GetItem (intposition) { -             returnContacts.get (position); About         } $  - @Override -          PublicView GetView (intposition, View Convertview, ViewGroup parent) { -  A             if(Convertview = =NULL) { +Convertview = inflater.inflate (Resource,NULL); the             } -  $Contact Contact =GetItem (position); the  theTextView Text1 =(TextView) Convertview.findviewbyid (Android. R.ID.TEXT1); theTextView Text2 =(TextView) Convertview.findviewbyid (Android. R.ID.TEXT2); the  -             //the first character, the basis of the grouping in Text1.settext (Contact.firstletterofname ()); the  the             //Details AboutText2.settext (Contact.name + "" +contact.getphonenumbers ()); the             returnConvertview; the         } the  +     } -  the     //General method for reading device contacts. The approximate process is this, the templated operation code. Bayi     Private voidReadcontacts (arraylist<contact>contacts) { theUri uri = uri.parse ("Content://com.android.contacts/contacts"); theContentresolver resolver = This. Getcontentresolver (); -  -         //here we pass in a sort_key_primary to query the         //tell Contentresolver the results are sorted alphabetically by the first letter of the contact name thecursor cursor = resolver.query (URI,NULL,NULL,NULL, the Android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY); the  -          while(Cursor.movetonext ()) { the             //Contact ID theString ID =cursor.getstring (Cursor.getcolumnindex (android.provider.contactscontract.contacts._id)); the             //Sort key, the read contact is grouped by name from A->z94String sork_key_primary =cursor the . GetString (Cursor.getcolumnindex (Android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY));  the             //get name of contact person theString name =cursor98 . GetString (Cursor.getcolumnindex (Android.provider.ContactsContract.Contacts.DISPLAY_NAME)); About  -Contact Mcontact =NewContact ();101Mcontact.id =ID;102Mcontact.sort_key_primay =sork_key_primary;103Mcontact.name =name;104  the             //get the phone number of the contact person106Cursor phone = resolver.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,107ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ID,NULL,NULL);108             //Get a phone number (there may be multiple numbers)109             //because under the same name, the user may have more than one number the             //Traverse111Arraylist<string> phonenumbers =NewArraylist<string>(); the              while(Phone.movetonext ()) {113                 intPhonefieldcolumnindex =Phone.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER); theString PhoneNumber =phone.getstring (phonefieldcolumnindex); the Phonenumbers.add (phonenumber); the             }117 118Mcontact.phonenumbers =phonenumbers;119 Contacts.add (mcontact); -         }121     }122 123     //used to mount data read from the contact database. 124     //structured data for easy data manipulation and access.  the     Private classContact {126          PublicString ID;127          PublicString name; -          PublicString Sort_key_primay;129          PublicArraylist<string>phonenumbers; the 131         //get the first letter of a contact's first name.  the         //For example, if a person's name is "Android", then this person's initials are: a133          PublicString Firstletterofname () {134String s = Sort_key_primay.charat (0) + "";135 136             returns.touppercase ();137         }138 139          PublicString getphonenumbers () { $String phones = "";141              for(inti = 0; I < phonenumbers.size (); i++) {142Phones + = ":" +Phonenumbers.get (i);143             }144             returnphones;145         }146     }147}

Permissions:

1 <!--  - 2     <android:name= " Android.permission.WRITE_CONTACTS "/>3     <!---- >4     <android:name= "Android.permission.READ_ CONTACTS "/>

Read contacts based on the first character of the contact name

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.