Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 如何獲取android手機聯系人並按字母展示(二)

如何獲取android手機聯系人並按字母展示(二)

編輯:關於Android編程

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

在adapter implement SectionIndexer

這樣adapter裡必須實現以下3個接口:

		@Override
		public Object[] getSections() { //section的集合
			
		}

		@Override
		public int getPositionForSection(int section) {//當前section的位置
			
		}

		@Override
		public int getSectionForPosition(int position) {//當前position的section信息
			
		}

所以我們必須把取到的本地聯系人的數據進行加工

定義一個來存儲section的信息:

 private class SectionTitle{
    	public String title;

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


定義以下結構來獲取實現sectionIndexer所要的信息:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;"> 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; } @Override public Object[] getSections() { return mSections; } /** * 找出這個section對應的position */ @Override public int getPositionForSection(int section) { if (mSectionPositionMap == null) return -1; if (section == 0) return -1; return mSectionPositionMap.get(section, -1); } /** * 找出這個position對應的section */ @Override public 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改變都會調用)這個方法,進行加工,取出數據的首字母信息

		@Override
		public 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);
		}

		@Override
		public Object[] getSections() {
			return mSectionMapper.getSections();
		}

		@Override
		public int getPositionForSection(int section) {
			return mSectionMapper.getPositionForSection(section);
		}

		@Override
		public int getSectionForPosition(int position) {
			return mSectionMapper.getSectionForPosition(position);
		}

效果圖:


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved