The list of Android contacts on the Internet is also very many, and the contact is almost the same, because the project used the Contact List index function (the product to the letter item is removed), but also good implementation. Here I also share my realization, lest I forget later. Let's look at the effect first (demo at the end):
The effect is so simple.
First, let's talk about the idea: first add a pinyin field to the contact object, and when you get the original contact data, convert each contact's name to pinyin. and set the value for the Pinyin field.
Then get the pinyin for the letters that appear in the contacts to be saved as an array (this is the letter item), and then group the new arrays with the contact pinyin again. With the Arrays.sort sorting function, the contact list is then sorted alphabetically, followed by the location of the letters in the contact list to be saved, the right alphabetical index, the corresponding letter set the current letter in the contact list position, sliding to a certain letter is, the ListView moved to that position It is.
One: First look at the letters that appear in the extraction contacts and sort
/** * Sort the data. and add a A-Z order in * * @param cartypes * @return */public string[] SortIndex (list<constastbean> Constastbeans) {treeset< string> set = new treeset<string> (); for (Constastbean constastbean:constastbeans) {Char ch = constastbean.getpi Nyin (). CharAt (0), Set.add (string.valueof (CH). toUpperCase (Locale.getdefault ()));//Get the first letter that appears}string[] names = new String[constastbeans.size () + set.size ()];//new array to hold the first letter + contact Pinyin int i = 0;for (String string:set) {//Add the letters in the set to the new array (front (face) names[i] = string;i++;} <span style= "White-space:pre" ></span>//to extract the contact pinyin into an array string[] Pyheader = new String[constastbeans.size ( )];for (int j = 0; J < Constastbeans.size (); j + +) {Pyheader[j] = Constastbeans.get (j). Getpinyin (); System.arraycopy (pyheader, 0, names, Set.size (), pyheader.length);//<span style= "font-family:arial, Helvetica, Sans-serif; " > To add the contact pinyin to the back, the result is the contact pinyin and the occurrence of the first letter in an array (is unordered) </span>//their own initiative according to the first alphabetical order Arrays.sort (names, String.case_ Insensitive_order);//Sort strictly alphabetically, ignoring lettersUppercase and lowercase, resulting in an array sorted by pinyin returns return names;} This return is ordered alphabetically (such as [A, Anne, G, Galen ... Z]).
Second: The next step is to sort the contact objects according to the order of return.
/** * Sort data by name because the default is the number in the first place, in order to put the number to the end, you need to convert * * @param arry * @return */public arraylist<constastbean> Getallli STS (string[] arry) {arraylist<constastbean> lists = new arraylist<constastbean> ();//Save the sorted data ArrayList <ConstastBean> lists2 = new arraylist<constastbean> ();//save data at the beginning of a number arraylist<constastbean> LISTS3 = New arraylist<constastbean> ();//Save alphabetic Data//Sort data for (int i = 0; i < arry.length; i++) {for (int j = 0; J < Sourc Edata.size (); J + +) {if (Arry[i].equals (Sourcedata.get (j). Getpinyin ())) {Lists.add (Sourcedata.get (j)); break;} else//need to display a single letter of item, there is no need to stare. In adapter, you should set a separate layout for this item//{//constastbean Contactbean = new Constastbean ();//contactbean.setpinyin (Arry[i]);// Contactbean.setnickname (Arry[i]);//lists.add (Contactbean);//break;//}}}//separate digital data and alphabetic data int index = getletter (lists) ;//Gets the position of the beginning of the letter for (int i = 0; i < lists.size (); i++) {if (I < index) {Lists2.add (Lists.get (i));} Else{lists3.add (Lists.get (i));}} Lists.clear (); Lists.addall (LISTS3); Lists.addall (lists2); return lists;}
Because the number is in front of the letter, but there is a number in the back, so first query the position of the first letter appears. It is then intercepted from that location as 2 sets. Add the set of numbers to the back of the letter set.
Three: The initialization work is done. It's time to traverse to get the position of each letter.
<span style= "White-space:pre" ></span>selector = new hashmap<string, integer> ();//traverse the sorted data, Gets the position of each letter for (int i = 0; i < indexstr.length; i++) {for (int j = 0; J < datas.size (); + j) {if (Datas.get (j). Getpin Yin (). toLowerCase (Locale.getdefault ()). StartsWith (Indexstr[i<span style= "White-space:pre" ></SPAN>]. toLowerCase (Locale.getdefault ()))) {Selector.put (indexstr[i], j); break;} String Pinyin = Datas.get (j). Getpinyin (); if (Indexstr[i].equals ("#") && IsNumeric (pinyin.substring (0,1))) { Selector.put (Indexstr[i], j); return;}}
The returned results are similar to this: {d=2, #=23, e=4, g=5, a=0, l=11, m=12, n=14, h=6, J=8, k=10, t=19, v=20, s=16, r=15, z=21}
/** * Draw index strip */public void Drawindexview () {linearlayout.layoutparams params = new Layoutparams (layoutparams.match_parent , height); for (int i = 0; i < indexstr.length; i++) {TextView TV = new TextView (this); Tv.setlayoutparams (params); Tv.set Text (Indexstr[i]); tv.setgravity (Gravity.center); Tv.settextcolor (This.getresources (). GetColor (R.color.indexs_ color); tv.settextsize; Layoutindex.addview (TV); Layoutindex.setontouchlistener (new Ontouchlistener () {@ Overridepublic boolean OnTouch (View V, motionevent event) {//TODO auto-generated method Stubfloat y = event.gety (); int in Dex = (int) y/height;//Gets the index of the clicked letter position string key = ""; if (Index < indexstr.length && index >-1) {key = Index Str[index];if (Selector.containskey (key)) {int position = Selector.get (key); if (Listview.getheaderviewscount () > 0) {//Added header to remove listview.setselectionfromtop (position + listview.getheaderviewscount (), 0);} else {listview.setselectionfromtop (position, 0);//swipe to the first item}}if (Key.equals ("↑")) {ListView. Setselectionfromtop (0, 0);//slide to the first}}if (!key.equals ("")) {Showtv.settext (key); Showtv.setvisibility (view.visible) ;} Switch (event.getaction ()) {case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:case motionevent.action_ OUTSIDE:showTv.setVisibility (View.gone); Break;case motionevent.action_down://Layoutindex.setbackground (); break;} return true;}});}}That's it. The implementation of the indexed contact list:Demo source code download
Android with indexed contacts list