如何获得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);
- }
效果图:
版权声明:本文博主原创文章。博客,未经同意不得转载。
如何获得android手机通讯录的字母显示(两)的更多相关文章
- python实现将android手机通讯录vcf文件转化为csv
经常会遇到将手机通讯录导出到电脑并转化为在电脑中可编辑的情况,在网上搜索了很久当前不外乎两种处理方式.1.使用电脑的outlook的通讯簿功能,将手机导出的vcf文件导入到outlook的通讯录中,然 ...
- Android 开发学习进程0.17 Android资源文件selector textview显示两种不同字体
selector 是安卓资源文件的一种,它可以使按钮等实现不同状态下的不同UI,不用在代码中实现,而使用方式有两种,一种在color文件下 创建.xml可以使按钮等字体在不同状态下的变化,其二是在dr ...
- 怎样获取android手机联系人并按字母展示(一)
android提供了本地数据库的查询uri,能够查询出数据: 採用一个AsyncQueryHandler来进行查询, AsyncQueryHandler自己开启了线程来进行数据查询,非常方便 prot ...
- 怎样获取android手机联系人并按字母展示(三)
假设获取contact的头像信息并展示: 怎样依据photoId来获取bitmap: public static Bitmap getContactPhoto(Context context, lon ...
- Qt for Android(一)Qt在Android手机上全屏显示
1.在程序的编译路径下找到AndroidManifest.xml文件,具体路径如下: 2.在pro文件中添加一句话: ANDROID_PACKAGE_SOURCE_DIR = $$PWD/Androi ...
- ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片
解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...
- 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。
package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...
- Android程序设计-简单手机通讯录
在微信中,手机QQ中你会发现软件读取手机通讯录这个功能,这个功能使得软件更好的与手机联系人绑定,从而达到分享,拨打电话,读取用户信息等操作.下面我们将通过一个demo实现这个功能 首先我们看一下效果图 ...
- 【Android Demo】简单手机通讯录
Android 系统给我们提供了访问通讯录的接口,通过接口获取通讯录信息.Adapter 与 View 的连接主要依靠 getView 这个方法返回我们需要的自定义 view. ListView 是 ...
随机推荐
- Memento pattern
21.5 再谈备忘录的封装 备忘录是一个很特殊的对象,只有原发器对它拥有控制的权力,负责人只负责管理,而其他类无法访问到备忘录,因此我们需要对备忘录进行封装. 为了实现对备忘录对象的封装,需要对备忘录 ...
- Ewebeditor最新漏洞和漏洞指数
Ewebeditor最新漏洞和漏洞指数[收集] 来源:转载作者:佚名时间:2009-06-03 00:04:26 下面文章收集转载于网络:) 算是比較全面的ewebeditor编辑器的漏洞收集,如今的 ...
- WinForm 国际化开发一例
1.新建一个WinForm程序(在中文版的Windows下),如下,添加1个Button和1个Label 设置Form1的localizable属性为True(Form1的properties里的De ...
- Sliverlight之 矢量绘图
目标:在两天内完成一个环形图的绘制 准备:第5章 矢量绘图 1,形状绘图(见Project11) (1)线条用什么标签表示,它有哪几个重要属性 说明: Line标签 x1 y1表示起始点x,y坐标 x ...
- Javascript中的__proto__、prototype、constructor
今天重温了下Javacript,给大家带来一篇Javascript博文,相信对于Javacript有一定了解的人都听过prototype原型这个概念,今天我们深度的分析下prototype与__pro ...
- SQL查询优化——数据结构设计
本文部分内容会涉及mysql,可能在其它数据库中并不适用. 本章节仅仅针对数据库结构设计做讨论.查询优化的其它内容待续. 数据库设计及使用是WEB开发程序猿必备的一项基础技能,在大数据量和高并发场景, ...
- fullcalendar日历控件集合知识
1.基本的语法: 首先,fullcalendar和JQUERY一样,以面向对象的方式来组织代码.当然,这里的面向对象不过指能够把整个fullcalendar理解为一个类,这个类里包含有非常多的属性.方 ...
- define a class for a linked list and write a method to delete the nth node.
1.问题 define a class for a linked list and write a method to delete the nth node. 2.算法 template <t ...
- Snail—ORACLE基础之事务学习(五)
---------------事务---当运行到commit时 事务才算是完毕,不然 会运行rollback操作. declare v_money acount.money%type:=1223; e ...
- Oracle Product Hub / Product Lifecycle Management / Product Information Management / Advanced Produc
In this Document Goal Solution 1. Master List showing sample code for APIs in Product Data Hub ...