How to get android mobile phone contacts and display them in letters (2)
The following describes how to categorize and display contacts with the same initials:
In the adapter implement SectionIndexer
In this way, the adapter must implement the following three interfaces:
@ Overridepublic Object [] getSections () {// collection of sections} @ Overridepublic int getPositionForSection (int section) {// location of the current section} @ Overridepublic int getSectionForPosition (int position) {// section information of the current position}
Therefore, we must process the data of the obtained local contact.
Define a section to store information:
private class SectionTitle{ public String title; public SectionTitle(){ title = "\"; } public String toString(){ return title; } }
Define the following structure to obtain the information required to implement sectionIndexer: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Signature = "brush: java;"> private class ContactSectionMapper implements SectionIndexer {private SectionTitle [] mSections = null; private SparseIntArray mSectionPositionMap = null; private SparseIntArray Signature = null; public Functions (SectionTitle [] sectionDatas) {mSections = sectionDatas;} public void changeData (SparseIntArray sectionPositionMap, SparseIntArray positionSectionMap) {region = sectionPositionMap; region = positionSectionMap ;} @ Overridepublic Object [] getSections () {return mSections;}/*** locate the position corresponding to this section */@ Overridepublic int getPositionForSection (int section) {if (mSectionPositionMap = null) return-1; if (section = 0) return-1; return mSectionPositionMap. get (section,-1);}/*** locate the section corresponding to 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 = getPositionForSection (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 = getSectionForPosition (position ); if (sectionIndex <0 | sectionIndex> = mSections. length) return NONE_ENGLISH_LETTER_TITLE; return mSections [sectionIndex]. toString ();}}
In the adapter, we reload the changeCursor method (called every time the cursor changes) to process and retrieve the initial information of the data.
@ 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 () {mSection Mapper. changeData (sectionPositionMap, positionSectionMap); return;} String curtitle = "\"; int I = 0; int position = 0; while (c. moveToNext () {position = c. getPosition (); String curLetter = getTitle (getDisplayName (c); // you can check whether the first name of the current name is the beginning of the section. if (TextUtils. isEmpty (curtitle) "|! TextUtils. equals (curLetter, curtitle) {mSectionDatas [I]. title = curLetter; // The current name is sectionPositionMap at the beginning of section. 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 );}
In adapter initialization:
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);}
@Overridepublic Object[] getSections() {return mSectionMapper.getSections();}@Overridepublic int getPositionForSection(int section) {return mSectionMapper.getPositionForSection(section);}@Overridepublic int getSectionForPosition(int position) {return mSectionMapper.getSectionForPosition(position);}
: