首页 > 代码库 > 如何获取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所要的信息:

	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改变都会调用)这个方法,进行加工,取出数据的首字母信息

<span style="white-space:pre">		</span>@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);
		}

<span style="white-space:pre">		</span>@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);
		}

效果图: