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

于adapter implement SectionIndexer

这项adapter必须在下面可以实现3接口:

  1. @Override
  2. public Object[] getSections() { //section的集合
  3.  
  4. }
  5.  
  6. @Override
  7. public int getPositionForSection(int section) {//当前section的位置
  8.  
  9. }
  10.  
  11. @Override
  12. public int getSectionForPosition(int position) {//当前position的section信息
  13.  
  14. }

所以我们必须把取到的本地联系人的数据进行加工

定义一个来存储section的信息:

  1. private class SectionTitle{
  2. public String title;
  3.  
  4. public SectionTitle(){
  5. title = "";
  6. }
  7.  
  8. public String toString(){
  9. return title;
  10. }
  11. }

定义下面结构来获取实现sectionIndexer所要的信息:

  1. private class ContactSectionMapper implements SectionIndexer {
  2.  
  3. private SectionTitle[] mSections = null;
  4. private SparseIntArray mSectionPositionMap = null;
  5. private SparseIntArray mPositionSectionMap = null;
  6.  
  7. public ContactSectionMapper(SectionTitle[] sectionDatas) {
  8. mSections = sectionDatas;
  9. }
  10.  
  11. public void changeData(SparseIntArray sectionPositionMap, SparseIntArray positionSectionMap) {
  12. mSectionPositionMap = sectionPositionMap;
  13. mPositionSectionMap = positionSectionMap;
  14. }
  15.  
  16. @Override
  17. public Object[] getSections() {
  18. return mSections;
  19. }
  20.  
  21. /**
  22. * 找出这个section相应的position
  23. */
  24. @Override
  25. public int getPositionForSection(int section) {
  26. if (mSectionPositionMap == null)
  27. return -1;
  28.  
  29. if (section == 0)
  30. return -1;
  31.  
  32. return mSectionPositionMap.get(section, -1);
  33. }
  34.  
  35. /**
  36. * 找出这个position相应的section
  37. */
  38. @Override
  39. public int getSectionForPosition(int position) {
  40. if (mPositionSectionMap == null)
  41. return -1;
  42.  
  43. if (position <= 0)
  44. return 0;
  45.  
  46. return mPositionSectionMap.get(position, -1);
  47. }
  48.  
  49. /**
  50. * @param position
  51. * @return
  52. */
  53. public boolean isSection(int position) {
  54. if (position == 0)
  55. return true;
  56.  
  57. int sectionIdx = getSectionForPosition(position);
  58. int sectionPosition = getPositionForSection(sectionIdx);
  59.  
  60. if (sectionIdx == -1 && sectionPosition == -1)
  61. return false;
  62.  
  63. return (position == sectionPosition);
  64. }
  65.  
  66. public String getSection(int position) {
  67. if (mSections == null)
  68. return NONE_ENGLISH_LETTER_TITLE;
  69.  
  70. int sectionIndex = getSectionForPosition(position);
  71. if (sectionIndex < 0 || sectionIndex >= mSections.length)
  72. return NONE_ENGLISH_LETTER_TITLE;
  73.  
  74. return mSections[sectionIndex].toString();
  75. }
  76.  
  77. }

在adapter里面我们重载changeCursor(每次cursor改变都会调用)这种方法。进行加工,取出数据的首字母信息

  1. <span style="white-space:pre"> </span>@Override
  2. public void changeCursor(Cursor c) {
  3. processCursor(c);
  4. super.changeCursor(c);
  5. }
  6.  
  7. private void processCursor(Cursor c) {
  8.  
  9. /** define some variables */
  10. SparseIntArray sectionPositionMap = new SparseIntArray();
  11. SparseIntArray positionSectionMap = new SparseIntArray();
  12.  
  13. for(int i = 0; i < mSectionDatas.length; i++){
  14. mSectionDatas[i].title = "";
  15. }
  16.  
  17. if (c == null || c.getCount() == 0 || c.isClosed()){
  18. mSectionMapper.changeData(sectionPositionMap, positionSectionMap);
  19. return;
  20. }
  21.  
  22. String curtitle = "";
  23.  
  24. int i = 0;
  25. int position = 0;
  26. while (c.moveToNext()) {
  27. position = c.getPosition();
  28.  
  29. String curLetter = getTitle(getDisplayName(c));
  30.  
  31. //看看当前的名字的第一个名字是什么。是不是section的开头
  32. if (TextUtils.isEmpty(curtitle) || !TextUtils.equals(curLetter, curtitle)) {
  33. mSectionDatas[i].title = curLetter;
  34. //这个当前的名字是section的开头
  35. sectionPositionMap.put(i, position);
  36. curtitle = curLetter;
  37. i++;
  38. }
  39. positionSectionMap.put(position, i - 1);
  40. }
  41.  
  42. for(; i < mSectionDatas.length; i++){
  43. mSectionDatas[i].title = curtitle;
  44. sectionPositionMap.put(i, position);
  45. }
  46. mSectionMapper.changeData(sectionPositionMap, positionSectionMap);
  47. }

在adapter的初始化里面:

  1. protected final class ContactsAdapter extends ResourceCursorAdapter implements SectionIndexer,OnScrollListener {
  2.  
  3. protected boolean mLoading = true;
  4. private ContactSectionMapper mSectionMapper = null;
  5. private SectionTitle[] mSectionDatas = null;
  6. private static final int SECTION_COUNT = 27;
  7.  
  8. public ContactsAdapter(Context context) {
  9. super(context, R.layout.contacts_list_item_photo,null);
  10.  
  11. mSectionDatas = new SectionTitle[SECTION_COUNT];
  12. for (int i = 0; i < SECTION_COUNT; i++) {
  13. mSectionDatas[i] = new SectionTitle();
  14. }
  15. mSectionMapper = new ContactSectionMapper(mSectionDatas);
  16. }

  1. <span style="white-space:pre"> </span>@Override
  2. public Object[] getSections() {
  3. return mSectionMapper.getSections();
  4. }
  5.  
  6. @Override
  7. public int getPositionForSection(int section) {
  8. return mSectionMapper.getPositionForSection(section);
  9. }
  10.  
  11. @Override
  12. public int getSectionForPosition(int position) {
  13. return mSectionMapper.getSectionForPosition(position);
  14. }

效果图:

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

如何获得android手机通讯录的字母显示(两)的更多相关文章

  1. python实现将android手机通讯录vcf文件转化为csv

    经常会遇到将手机通讯录导出到电脑并转化为在电脑中可编辑的情况,在网上搜索了很久当前不外乎两种处理方式.1.使用电脑的outlook的通讯簿功能,将手机导出的vcf文件导入到outlook的通讯录中,然 ...

  2. Android 开发学习进程0.17 Android资源文件selector textview显示两种不同字体

    selector 是安卓资源文件的一种,它可以使按钮等实现不同状态下的不同UI,不用在代码中实现,而使用方式有两种,一种在color文件下 创建.xml可以使按钮等字体在不同状态下的变化,其二是在dr ...

  3. 怎样获取android手机联系人并按字母展示(一)

    android提供了本地数据库的查询uri,能够查询出数据: 採用一个AsyncQueryHandler来进行查询, AsyncQueryHandler自己开启了线程来进行数据查询,非常方便 prot ...

  4. 怎样获取android手机联系人并按字母展示(三)

    假设获取contact的头像信息并展示: 怎样依据photoId来获取bitmap: public static Bitmap getContactPhoto(Context context, lon ...

  5. Qt for Android(一)Qt在Android手机上全屏显示

    1.在程序的编译路径下找到AndroidManifest.xml文件,具体路径如下: 2.在pro文件中添加一句话: ANDROID_PACKAGE_SOURCE_DIR = $$PWD/Androi ...

  6. ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片

    解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...

  7. 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。

      package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...

  8. Android程序设计-简单手机通讯录

    在微信中,手机QQ中你会发现软件读取手机通讯录这个功能,这个功能使得软件更好的与手机联系人绑定,从而达到分享,拨打电话,读取用户信息等操作.下面我们将通过一个demo实现这个功能 首先我们看一下效果图 ...

  9. 【Android Demo】简单手机通讯录

    Android 系统给我们提供了访问通讯录的接口,通过接口获取通讯录信息.Adapter 与 View 的连接主要依靠 getView 这个方法返回我们需要的自定义 view. ListView 是 ...

随机推荐

  1. Memento pattern

    21.5 再谈备忘录的封装 备忘录是一个很特殊的对象,只有原发器对它拥有控制的权力,负责人只负责管理,而其他类无法访问到备忘录,因此我们需要对备忘录进行封装. 为了实现对备忘录对象的封装,需要对备忘录 ...

  2. Ewebeditor最新漏洞和漏洞指数

    Ewebeditor最新漏洞和漏洞指数[收集] 来源:转载作者:佚名时间:2009-06-03 00:04:26 下面文章收集转载于网络:) 算是比較全面的ewebeditor编辑器的漏洞收集,如今的 ...

  3. WinForm 国际化开发一例

    1.新建一个WinForm程序(在中文版的Windows下),如下,添加1个Button和1个Label 设置Form1的localizable属性为True(Form1的properties里的De ...

  4. Sliverlight之 矢量绘图

    目标:在两天内完成一个环形图的绘制 准备:第5章 矢量绘图 1,形状绘图(见Project11) (1)线条用什么标签表示,它有哪几个重要属性 说明: Line标签 x1 y1表示起始点x,y坐标 x ...

  5. Javascript中的__proto__、prototype、constructor

    今天重温了下Javacript,给大家带来一篇Javascript博文,相信对于Javacript有一定了解的人都听过prototype原型这个概念,今天我们深度的分析下prototype与__pro ...

  6. SQL查询优化——数据结构设计

    本文部分内容会涉及mysql,可能在其它数据库中并不适用. 本章节仅仅针对数据库结构设计做讨论.查询优化的其它内容待续. 数据库设计及使用是WEB开发程序猿必备的一项基础技能,在大数据量和高并发场景, ...

  7. fullcalendar日历控件集合知识

    1.基本的语法: 首先,fullcalendar和JQUERY一样,以面向对象的方式来组织代码.当然,这里的面向对象不过指能够把整个fullcalendar理解为一个类,这个类里包含有非常多的属性.方 ...

  8. 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 ...

  9. Snail—ORACLE基础之事务学习(五)

    ---------------事务---当运行到commit时 事务才算是完毕,不然 会运行rollback操作. declare v_money acount.money%type:=1223; e ...

  10. 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 ...