Grouping and letter prompting effects of system contact software for Android

Source: Internet
Author: User
Tags number sign

The contact is displayed in different chapters and the example of the first letter of the contact is quickly displayed in listview. After reading many such examples on the Internet, it is found that it is generally extracted from the system source code and is generally complicated, it is simplified, highly scalable, and very convenient to transplant.


1. fastcontactsearchdemoactivity. Java

Package COM. zhf. fastcontactsearchdemo; import Java. util. arraylist; import Java. util. collections; import Java. util. hashmap; import Java. util. list; import Java. util. set; import Java. util. regEx. pattern; import android. app. activity; import android. content. asyncqueryhandler; import android. content. contentresolver; import android. content. contentvalues; import android. content. context; import android. database. cursor; I Mport android.net. uri; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. listview; import android. widget. sectionindexer; import android. widget. textview;/*** contact Chapter display and listview quickly slide show contact first letter example * This example from the http://blog.csdn.net/luck_apple/article/details/6741860 * to view a lot of such examples on the Internet, it is found that the commonly extracted from the system source code And is generally complicated. It is simplified, highly scalable, and very convenient to transplant. * @ Author hiphonezhu@sina.com **/public class fastcontactsearchdemoactivity extends activity {private baseadapter adapter; private listview personlist; private asyncqueryhandler asyncquery; Private Static final string name = "name ", number = "Number", sort_key = "sort_key"; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); person List = (listview) findviewbyid (R. id. listview); asyncquery = new myasyncqueryhandler (getcontentresolver ();} @ overrideprotected void onresume () {super. onresume (); Uri uri = Uri. parse ("content: // COM. android. contacts/data/phones "); // contact's uristring [] projection = {" _ id "," display_name "," data1 "," sort_key "}; // The queried column asyncquery. startquery (0, null, Uri, projection, null, null, "sort_key collate localized ASC "); // query by sort_key in ascending order}/*** database asynchronous Query Class asyncqueryhandler ** @ author administrator **/private class myasyncqueryhandler extends asyncqueryhandler {public handler (contentresolver Cr) {super (CR) ;}/ *** callback function for query completion */@ overrideprotected void onquerycomplete (INT token, object cookie, cursor) {If (cursor! = NULL & cursor. getcount ()> 0) {list <contentvalues> List = new arraylist <contentvalues> (); cursor. movetofirst (); For (INT I = 0; I <cursor. getcount (); I ++) {contentvalues CV = new contentvalues (); cursor. movetoposition (I); string name = cursor. getstring (1); string number = cursor. getstring (2); string sortkey = cursor. getstring (3); If (number. startswith ("+ 86") {// remove the redundant China region number sign, which has no effect on this program. CV. put (name, name); cv. put (number, number. substring (3); cv. put (sort_key, sortkey);} else {cv. put (name, name); cv. put (number, number); cv. put (sort_key, sortkey);} List. add (CV);} If (list. size ()> 0) {setadapter (list) ;}}} private void setadapter (list <contentvalues> List) {adapter = new listadapter (this, list); personlist. setadapter (adapter);} Private Static class viewholder {textview Alpha; textview name; Tex Tview number;}/*** when using other projects, you only need to upload an ordered list */private class listadapter extends baseadapter implements sectionindexer {private layoutinflater Inflater; private list <contentvalues> list; private hashmap <string, integer> alphaindexer; // Save the position of each index in the list [#-0, A-4, B-10] private string [] sections; // index table for each group [a, B, c, f...] public listadapter (context, list <contentvalues> List) {This. inflater = layoutinfl Ater. From (context); this. List = List; // This list is a sorted set. Data in some projects must be sorted by yourself. This. alphaindexer = new hashmap <string, integer> (); For (INT I = 0; I <list. size (); I ++) {string name = getalpha (list. get (I ). getasstring (sort_key); If (! Alphaindexer. containskey (name) {// only records the first occurrence location alphaindexer in the list. put (name, I) ;}}set <string> sectionletters = alphaindexer. keyset (); arraylist <string> sectionlist = new arraylist <string> (sectionletters); collections. sort (sectionlist); sections = new string [sectionlist. size ()]; sectionlist. toarray (sections) ;}@ overridepublic int getcount () {return list. size () ;}@ overridepublic object getitem (INT position) {r Eturn list. get (position) ;}@ overridepublic long getitemid (INT position) {return position ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {viewholder holder; if (convertview = NULL) {convertview = Inflater. inflate (R. layout. list_item, null); holder = new viewholder (); holder. alpha = (textview) convertview. findviewbyid (R. id. alpha); holder. name = (textview) convertview. find Viewbyid (R. id. name); holder. number = (textview) convertview. findviewbyid (R. id. number); convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} contentvalues CV = List. get (position); string name = CV. getasstring (name); string number = CV. getasstring (number); holder. name. settext (name); holder. number. settext (number); // sortkeystring currentstr = getalpha (list. get (position ). geta Sstring (sort_key); // sortkeystring previewstr = (position-1)> = 0? Getalpha (list. Get (position-1). getasstring (sort_key): "";/*** identify the display #, A-Z textview hidden and visible */If (! Previewstr. Equals (currentstr) {// sortkey of the current contact! = Sortkey of the previous contact, indicating that the current contact is in a new group. Holder. alpha. setvisibility (view. visible); holder. alpha. settext (currentstr);} else {holder. alpha. setvisibility (view. gone);} return convertview;}/** this method returns the position in the list based on the first letter of the contact */@ overridepublic int getpositionforsection (INT Section) {string later = sections [section]; return alphaindexer. get (later);}/** in this example, you can ignore this method */@ overridepublic int getsectionforposition (INT position) {string key = getalpha (list. get (position ). getasstring (sort_key); For (INT I = 0; I <sections. length; I ++) {If (sections [I]. equals (key) {return I ;}} return 0 ;}@ overridepublic object [] getsections () {return sections ;}}/*** extract the first letter of an English letter, use # Instead of ** @ Param Str * @ return */private string getalpha (string Str) {If (STR = NULL) {return "#";} if (Str. trim (). length () = 0) {return "#";} Char c = Str. trim (). substring (0, 1 ). charat (0); // regular expression to determine whether the first letter is the English letter pattern = pattern. compile ("^ [A-Za-Z] + {1} quot;); If (pattern. matcher (C + ""). matches () {return (C + ""). touppercase (); // uppercase output} else {return "#";}}}

2. layout File

2.1.main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/listView"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:fastScrollEnabled="true" /></LinearLayout>

2.2.list _ item. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <! -- Initials --> <textview Android: Id = "@ + ID/Alpha" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: background = "#333333" Android: paddingleft = "10dip" Android: textcolor = "# ffffff" Android: visibility = "gone"/> <! -- Contact Information --> <imageview Android: Id = "@ + ID/imageview" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparentleft = "true" Android: layout_below = "@ ID/Alpha" Android: src = "@ drawable/ic_launcher"/> <textview Android: id = "@ + ID/Name" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_aligntop = "@ ID/imageview" Android: layout_margi Nleft = "2.0dip" Android: layout_marginright = "5.0dip" Android: layout_margintop = "6.0dip" Android: layout_torightof = "@ ID/imageview" Android: singleline = "true" Android: textappearance = "? Android: textappearancemedium "/> <textview Android: Id =" @ + ID/number "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: layout_alignleft = "@ ID/Name" Android: layout_alignwithparentifmissing = "true" Android: layout_below = "@ ID/Name" Android: ellipsize = "marquee" Android: singleline = "true" Android: textappearance = "? Android: textappearancesmall "/> </relativelayout>

3.


Related Article

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.