The subsequent write how to make each first letter display the same classification contact:
In adapter implement Sectionindexer
This adapter must be implemented below to implement the 3 interface:
@Overridepublic object[] GetSections () {//section's collection} @Overridepublic int getpositionforsection (int section) {// Position of current section} @Overridepublic int getsectionforposition (int position) {//current section information for position}
So we have to process the data from the local contacts.
Define a message to store the section:
Private class sectiontitle{public String title; Public Sectiontitle () { title = ""; } Public String toString () { return title; } }
Define the following structure to get the information required to implement Sectionindexer:
Private class Contactsectionmapper implements Sectionindexer {private sectiontitle[] msections = null;private Sparseintarray Msectionpositionmap = null;private Sparseintarray Mpositionsectionmap = null;public Contactsectionmapper (sectiontitle[] sectiondatas) {msections = Sectiondatas;} public void Changedata (Sparseintarray sectionpositionmap, Sparseintarray positionsectionmap) {Msectionpositionmap = Sectionpositionmap;mpositionsectionmap = Positionsectionmap;} @Overridepublic object[] GetSections () {return msections;} /** * Find this section corresponding position */@Overridepublic int getpositionforsection (int section) {if (Msectionpositionmap = = null) Return-1;if (section = = 0) return-1;return msectionpositionmap.get (section,-1);} /** * Find the corresponding section of this position */@Overridepublic int getsectionforposition (int position) {if (Mpositionsectionmap = = null) Return-1;if (position <= 0) return 0;return mpositionsectionmap.get (position,-1);} /** * @param position * @return */public boolean issection (int position) {if (position = = 0) return true;int sectionidx = getsectionforposition (position); int sectionposition = Getpositionforsect Ion (SECTIONIDX), if (Sectionidx = =-1 && sectionposition = =-1) return False;return (position = sectionposition);} Public String getSection (int position) {if (msections = = null) return none_english_letter_title;int Sectionindex = Getsect Ionforposition (position); if (Sectionindex < 0 | | sectionindex >= msections.length) return None_english_letter_ Title;return msections[sectionindex].tostring ();}}
In adapter we overload the Changecursor (which is called every time the cursor changes). To process, take out the first letter of the data
<span style= "White-space:pre" ></span> @Overridepublic void Changecursor (Cursor c) {processcursor (c); Super.changecursor (c);} private void Processcursor (Cursor c) {/** Define some variables */sparseintarray sectionpositionmap = new Sparseintarray () ; Sparseintarray Positionsectionmap = new Sparseintarray (); for (int i = 0; i < msectiondatas.length; i++) {msectiondatas[i ].title = "";} if (c = = NULL | | C.getcount () = = 0 | | c.isclosed ()) {Msectionmapper.changedata (Sectionpositionmap, Positionsectionmap); return;} String curtitle = ""; int i = 0;int position = 0;while (C.movetonext ()) {position = C.getposition (); String curletter = GetTitle (GetDisplayName (c));//See what the first name of the current name is. is not the beginning of the section if (Textutils.isempty (curtitle) | | |! Textutils.equals (Curletter, Curtitle)) {msectiondatas[i].title = curletter;// This current name is the beginning of the section sectionpositionmap.put (i, position); curtitle = curletter;i++;} Positionsectionmap.put (position, i-1);} for (; i < msectiondatas.length; i++) {msectiondatas[i].title = CurtiTle;sectionpositionmap.put (i, position);} Msectionmapper.changedata (Sectionpositionmap, positionsectionmap);}
Inside the initialization of the adapter:
Protected final class Contactsadapter extends Resourcecursoradapter implements Sectionindexer,onscrolllistener { protected Boolean mloading = true;private contactsectionmapper msectionmapper = null;private sectiontitle[] Msectiondatas = null;private static final int section_count = 27;public Contactsadapter (context context) {Super (context, R . layout.contacts_list_item_photo,null); Msectiondatas = new Sectiontitle[section_count];for (int i = 0; i < SECTION_ COUNT; i++) {Msectiondatas[i] = new Sectiontitle ();} Msectionmapper = new Contactsectionmapper (Msectiondatas);}
<span style= "White-space:pre" ></span> @Overridepublic object[] GetSections () {return Msectionmapper.getsections ();} @Overridepublic int getpositionforsection (int) {return msectionmapper.getpositionforsection (section);} @Overridepublic int getsectionforposition (int position) {return msectionmapper.getsectionforposition (position);}
:
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
How to get the letter display of the Android phone address Book (two)