如何擷取android手機連絡人並按字母展示(二)

來源:互聯網
上載者:User

標籤:contact   sectionindexer   

下面寫怎麼讓每個首字母相同的連絡人歸類展示:

在adapter implement SectionIndexer

這樣adapter裡必須實現以下3個介面:

@Overridepublic Object[] getSections() { //section的集合}@Overridepublic int getPositionForSection(int section) {//當前section的位置}@Overridepublic int getSectionForPosition(int position) {//當前position的section資訊}

所以我們必須把取到的本地連絡人的資料進行加工

定義一個來儲存section的資訊:

 private class SectionTitle{    public String title;    public SectionTitle(){    title = "";    }        public String toString(){    return title;    }    }


定義以下結構來擷取實現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;}/** * 找出這個section對應的position */@Overridepublic int getPositionForSection(int section) {if (mSectionPositionMap == null)return -1;if (section == 0)return -1;return mSectionPositionMap.get(section, -1);}/** * 找出這個position對應的section */@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();}}

在adapter裡面我們重載changeCursor(每次cursor改變都會調用)這個方法,進行加工,取出資料的首字母資訊

<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));//看看當前的名字的第一個名字是什麼,是不是section的開頭if (TextUtils.isEmpty(curtitle) || !TextUtils.equals(curLetter, curtitle)) {mSectionDatas[i].title = curLetter;//這個當前的名字是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);}

在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 section) {return mSectionMapper.getPositionForSection(section);}@Overridepublic int getSectionForPosition(int position) {return mSectionMapper.getSectionForPosition(position);}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.