Android with indexed contacts list

Source: Internet
Author: User
Tags addall locale

There are many examples of online Android contact list, and the contact person is similar, because the project used the Contact List index function (the product to remove the letter item), but also good implementation, here I also share my implementation, lest later forget, that first look at the effect (demo at the end of it):


The effect is so simple.

Let's talk first: Add a pinyin field to the contact object first, and when you get the contact raw data, convert the name of each contact 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 where the letters appear in the contact list, when the right alphabetical index is drawn, the letter is placed in the contact list, the position of the current letter is slid to a certain letter, and the ListView is moved to that position. Come on.


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 = consta Stbean.getpinyin (). CharAt (0), Set.add (string.valueof (CH). toUpperCase (Locale.getdefault ()));//Get the first letter that appears}string[] names = String[constastbeans.size () + set.size ()];//new array for holding the first letter + contacts Pinyin int i = 0;for (String string:set) {//Put in Set The letters are added to the new Array (front) 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; " > 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>//automatically sorted by the first letter arrays.sort (names, String.case_ Insensitive_order);//Sort strictly alphabetically, ignoring letter sizeWrite, the result is an array of alphabetical order 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, where there is no comment, you should set a separate layout for this item in adapter//{//constastbean Contactbean = new Constastbean ();// Contactbean.setpinyin (Arry[i]);//contactbean.setnickname (Arry[i]);//lists.add (Contactbean);//break;//}}}// Separates the numeric data and the 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 ordinal is the number in front of the letter, but here the number is required in the back, so first to query the location of the first letter, and then intercept from that position as 2 sets, and then add the number collection to the letter set after the line.

Three: The initialization work is done, it is the traversal to get each letter corresponding to the location

<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). Getpiny In (). 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;}});}}
This completes the implementation of the indexed contact list:Demo Source Download







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android with indexed contacts list

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.