码迷,mamicode.com
首页 > 移动开发 > 详细

如何获得android手机通讯录的字母显示(两)

时间:2015-09-30 19:32:41      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

随后的写如何使各第一字母显示相同的分类触点:

于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);
		}

效果图:

技术分享


版权声明:本文博主原创文章。博客,未经同意不得转载。

如何获得android手机通讯录的字母显示(两)

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/4850051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!