Android provides a query URI for the local database and can query out the data:
Adopt a Asyncqueryhandler to carry on the query, asyncqueryhandler oneself opens the thread to carry on the data query, very convenient
Protected Asyncqueryhandler mqueryhandler;protected final void querypersonal () {Mqueryhandler.startquery (QUERY_TOKEN , NULL, ContactsContract.Contacts.CONTENT_URI, personal.contacts_summary_projection, NULL, NULL, Getsortorder ( ContactsContract.Contacts.DISPLAY_NAME));} protected static string Getsortorder (String fieldName) {//substr is the Intercept function. Take the first letter//collate is primarily used to sort characters//collate localized sorted by local language return "case when substr (UPPER (" + FieldName + "), 1, 1) between ' A ' and ' Z ' then 1 else ten END, "+fieldname +" COLLATE localized ASC ";} Protected final class MyHandler extends Asyncqueryhandler {/** * Asynchronous Query handler constructor. */public Myhandl ER (context context) {super (Context.getcontentresolver ());} /** * on query completion. */@Override <strong>protected void onquerycomplete (int token, Object cookie, cursor cursor) </strong>{// Handler query completed callback if (cursor = = NULL | | cursor.isclosed ()) {return;} if (!isfinishing ()) {setloading (false); if (madapter! = null) {madapter.setloading (FALSE); madapter.changecursor (cursor);} if (cursor.getcount () = = 0) {memtytext.setvisibility (view.visible);} else {memtytext.setvisibility (view.invisible);}} else {if (cursor! = NULL &&!cursor.isclosed ()) {cursor.close ();}}}}
This layout is:
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:rcm= "http// Schemas.android.com/apk/res/com.ringcentral.android "android:id=" @+id/contact_list_view "android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" @color/bgcolormain "android:orientation=" V Ertical "> <listview android:id=" @android: Id/list "android:layout_width=" Match_parent "Androi d:layout_height= "Match_parent" android:cachecolorhint= "@android: Color/transparent" Android:descendantfocusab Ility= "afterdescendants" android:divider= "@null" android:fastscrollenabled= "true" Android:listselecto R= "@drawable/bg_list_item_selector"/> <relativelayout android:id= "@+id/no_contact_indication" Android Oid:layout_width= "Match_parent" android:layout_height= "match_parent" > <textview android:id= "@+id/emptylisttext" Android:layout_widTh= "Wrap_content" android:layout_height= "Wrap_content" android:layout_centerinparent= "true" Android:singleline= "true" android:text= "No Contacts" android:textcolor= "@color/text_no_items" Android:textsize= "20sp"/> <progressbar android:id= "@+id/loading" style= "@style /rcmprogressbar "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:layout_centerinparent= "true" android:visibility= "Gone"/> </relativelayout></linearlayo Ut>
Then the list adapter is written:
Protected static Final class Sectionedcontactlistitemcache {public TextView sectionheader;public TextView Nameview; Public TextView typeview;public ImageView photoview;public ImageView Detailitemicon; <span style= "White-space:pre" ></span>public View Nametypewrapper;} Protected final class Contactsadapter extends Resourcecursoradapter {public Contactsadapter (context context) {Super ( context, r.layout.contacts_list_item_photo,null);} @Overridepublic void Changecursor (Cursor c) {super.changecursor (c);} Protected string GetTitle (String displayName) {string title;/** Check if the first letter is 中文版 letter */matcher MATC her = Mpattern.matcher (DisplayName), if (!matcher.find ()) {title = None_english_letter_title;} else {title = Displayname.trim (). substring (0, 1). toUpperCase (locale.us);} return title;} Protected string GetDisplayName (Cursor c) {String displayName = c.getstring (Personal.name_column_index); Textutils.isempty (DisplayName)) {return "";} return displayName;} @Overridepublic void BindView (view view, context context, cursor cursor) {final Sectionedcontactlistitemcache cache = (Sectione Dcontactlistitemcache) View.gettag (); cache.typeView.setVisibility (View.gone); Cache.photoView.setVisibility ( view.visible); String name = cursor.getstring (Personal.name_column_index), if (Textutils.isempty (name)) {Cache.nameView.setText ( R.string.contact_no_name);} else {cache.nameView.setText (name);}} @Overridepublic View Newview (context context, cursor cursor, viewgroup parent) {View view = Super.newview (context, cursor, parent); final Sectionedcontactlistitemcache cache = new Sectionedcontactlistitemcache (); Cache.nametypewrapper = View.findviewbyid (r.id.name_type); cache.sectionheader = (TextView) View.findviewbyid ( R.id.txtsectionheader); Cache.nameview = (TextView) View.findviewbyid (r.id.name); Cache.typeview = (TextView) View.findviewbyid (r.id.type); Cache.photoview = (ImageView) View.findviewbyid (r.id.photo); Cache.detailitemicon = ( ImageView) view.findviewbyiD (R.id.contacts_detail_item_icon); View.settag (cache); return view;}} End of Adapter
Item Adapter Layout:
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent "android:layout_height=" wrap_content "android:orientation=" vertical "> <textview android:id=" @ +id/txtsectionheader "android:layout_width=" match_parent "android:layout_height=" @dimen/list_item_section_he Ight "android:background=" @drawable/bg_contacts_section_header "android:gravity=" Center_vertical|left " android:paddingleft= "@dimen/default_padding_to_side" android:textcolor= "@color/contacts_text_separator_text_ Color "android:textsize=" @dimen/font_size_medium "/> <relativelayout android:id=" @+id/contact_item " Android:layout_width= "Match_parent" android:layout_height= "@dimen/general_list_view_item_height" > <imageview android:id= "@+id/photo" android:layout_width= "@dimen/favorites_item_picture_width" Android:layout_height= "Match_paRent "android:layout_alignparentleft=" true "android:layout_alignwithparentifmissing=" true " android:paddingtop= "6DP" android:paddingbottom= "6DP" android:layout_marginleft= "@dimen/contact_pho To_margin_left_right "android:scaletype=" Fitcenter "/> <!--this icon is added in n Ext version, and now it would is hidden-<imageview android:id= "@+id/contacts_detail_item_icon" Android:layout_width= "50dip" android:layout_height= "60dip" Android:layout_alignparentrig Ht= "true" android:layout_marginright= "0dip" android:croptopadding= "true" Android:duplicat Eparentstate= "false" android:paddingbottom= "3dip" android:paddingtop= "3dip" Android:scale Type= "Fitcenter" android:src= "@drawable/ic_list_link" android:visibility= "Gone"/> <rel Ativelayout ANdroid:id= "@+id/name_type" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android:layout_alignwithparentifmissing= "true" android:layout_toleftof= "@id/contacts_detail_item_ico N "android:layout_marginleft=" @dimen/contact_photo_margin_left_right "android:layout_torightof=" @id/ Photo "android:background=" @drawable/bg_list_item_divider "> <textview android:i D= "@+id/type" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignbaseline= "@+id/name" android:layout_alignparentright= "true" an droid:layout_marginright= "@dimen/contact_type_margin_left_right" android:ellipsize= "marquee" Android:gravity= "Center_vertical|right" android:singleline= "true" android:textcolor= "@color /refresh_control_text_coLor "android:textsize=" @dimen/font_size_small "/> <textview android:id=" @+i D/name "android:layout_width=" match_parent "android:layout_height=" Wrap_content " Android:layout_centervertical= "true" android:layout_marginright= "@dimen/contact_name_margin_left_right" android:layout_toleftof= "@id/type" android:ellipsize= "marquee" Android:gravi Ty= "Center_vertical|left" android:singleline= "true" android:textcolor= "@color/text_color_mai N "android:textsize=" @dimen/font_size_medium "/> </RelativeLayout> </RELATIVELAYOUT&G T;</linearlayout>
How to get contacts for Android phones and show them alphabetically (i)