先看效果图:

要实现这个效果,activity必须实现ExpandableListActivity

	@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mContactListView = getExpandableListView();
mContactListView.setBackgroundResource(R.drawable.default_bg);
registerForContextMenu(mContactListView);
mContactDataBase = ((ContactApplication) getApplication())
.getmContactDataBase(); getExpandableListView().setCacheColorHint(0);// 拖动时避免出现黑色
getExpandableListView().setDivider(null);// 去掉每项以下的黑线(切割线)
// 自己定义下拉图标
getExpandableListView().setGroupIndicator(
getResources().getDrawable(R.drawable.expander_ic_folder));
setAdatperForExpandableListView();
} /**
* 设置ExpandableListView的adapter
*/
private void setAdatperForExpandableListView() {
Cursor groupCursor = mContactDataBase.getAllGroups(); //这个是从数据库里查询出全部的组
Util.d(TAG, "groupCursor=" + groupCursor);
// curosr的生命周期将和activity有关
startManagingCursor(groupCursor); // set my adapter
<strong>ContactTreeAdapter </strong>contactTreeAdapter = new ContactTreeAdapter(
groupCursor, this, true, mContactDataBase);
setListAdapter(contactTreeAdapter);
}

主要实现ContactTreeAdapter这个adapter

public class ContactTreeAdapter extends CursorTreeAdapter {

	/** log tag. */
private static final String TAG = "ContactTreeAdapter"; /** context */
public Context mContext = null;
private Cursor mCursor = null; private ContactDataBase mContactDataBase; // contact表字段索引
private static final int INDEX_NAME = 1;
private static final int INDEX_PHONENUMBER = 2; // group表字段索引
private static final int INDEX_GROUPNAME = 1; public ContactTreeAdapter(Cursor cursor, Context context,
boolean autoRequery, ContactDataBase contactDataBase) {
super(cursor, context, autoRequery);
mContext = context;
this.mContactDataBase = contactDataBase;
// TODO Auto-generated constructor stub
} @Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
// TODO Auto-generated method stub
String groupName = groupCursor.getString(INDEX_GROUPNAME);// 得到当前的组名
Cursor childCursor = mContactDataBase.getContactsByGroupName(groupName);
return childCursor;
} @Override
protected View newGroupView(Context context, Cursor cursor,
boolean isExpanded, ViewGroup parent) {
// TODO Auto-generated method stub
Util.d(TAG, "newGroupView");
LayoutInflater inflate = LayoutInflater.from(mContext);
View view = inflate.inflate(R.layout.grouplayout, null);
bindGroupView(view, context, cursor, isExpanded);
return view; } @Override
protected void bindGroupView(View view, Context context, Cursor cursor,
boolean isExpanded) {
// TODO Auto-generated method stub
Util.d(TAG, "bindGroupView");
TextView groupName = (TextView) view.findViewById(R.id.groupName);
String group = cursor.getString(INDEX_GROUPNAME);
groupName.setText(group); TextView groupCount = (TextView) view.findViewById(R.id.groupCount);
int count = mContactDataBase.getCountContactByGroupName(group);
Util.d(TAG, "count=" + count + "group=" + group);
groupCount.setText("[" + count + "]");
} @Override
protected View newChildView(Context context, Cursor cursor,
boolean isLastChild, ViewGroup parent) {
// TODO Auto-generated method stub
Util.d(TAG, "newChildView");
LayoutInflater inflate = LayoutInflater.from(mContext);
View view = inflate.inflate(R.layout.childlayout, null);
bindChildView(view, context, cursor, isLastChild);
return view;
} @Override
protected void bindChildView(View view, Context context, Cursor cursor,
boolean isLastChild) {
// TODO Auto-generated method stub
Util.d(TAG, "bindChildView cursor.getString(INDEX_PHONENUMBER)="
+ cursor.getString(INDEX_PHONENUMBER));
TextView name = (TextView) view.findViewById(R.id.name);
name.setText(cursor.getString(INDEX_NAME)); TextView description = (TextView) view.findViewById(R.id.description);
description.setTextKeepState(cursor.getString(INDEX_PHONENUMBER));
} }<strong>
</strong>

由于这个adapter的函数命名就能够看出是干什么的就不一一解释了

代码能够在http://download.csdn.net/detail/baidu_nod/7684649下载

android 支持分组和联系人展示的一个小样例的更多相关文章

  1. 利用jxl读取excel合并的单元格的一个小样例

    工作中我们可能要把Excel文件的记录保存到数据库, 今天我用jxl读取Excel文件时遇到了合并格的问题,记录例如以下: 如Excel文件例如以下: watermark/2/text/aHR0cDo ...

  2. Android开发自学笔记(Android Studio1.3.1)—2.开始第一个Android应用

    一.前言      使用Android Studio开发Android应用是一件非常简单的事情,因为它会帮你自动完成很多工作.本篇我们主要完成一个单击按钮在文本框显示当前时间的简单应用,借此来演示一下 ...

  3. 跟Google学习Android开发-起始篇-用碎片构建一个动态的用户界面(3)

    4.3 构建一个灵活的用户界面 当设计你的应用程序要支持大范围的屏幕尺寸时,你可以在不同的布局配置中重用碎片,来根据可用的屏幕空间优化用户体验. 例如,在手持设备上,它可能是适应来在一个单窗格用户界面 ...

  4. android面试题 不仅仅是面试是一个很好的学习

    下面的问题是在网上找到的总结,感谢您分享!希望,我们的共同进步,找到自己心仪的公司,: 1.android dvm 流程和Linux这个过程.无论是应用程序对同一概念: 答案:dvm是dalivk虚拟 ...

  5. Android - 支持不同的设备 - 支持不同的平台版本

    在最新版本的Android为app提供很好的新API时,也应该继续支持旧版本的Android直到大部分设备已经更新了.这里将要介绍如何在使用最新API带来的优点的同时继续支持老版本. Dashboar ...

  6. Android 手机卫士--获取联系人信息并显示与回显

    前面的文章已经实现相关的布局,本文接着进行相关的功能实现 本文地址:http://www.cnblogs.com/wuyudong/p/5951794.html,转载请注明出处. 读取系统联系人 当点 ...

  7. Android实现SQLite数据库联系人列表

    Android实现SQLite数据库联系人列表 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个通讯录查看程序: 要求使用SQLite ...

  8. Android实现图表绘制和展示

    本文演示在Android平台中绘制和展示图表示例,本示例是基于RChart 2实现的. 在一个系统中经常要用到图表统计数据,在WEB开发中图表绘制是一件简单的事情,因为有比较多的开源方案.但在Andr ...

  9. Android开发之读写联系人

    读写联系人需要用到android的ContentProvider 同时需要读和写联系人的权限 需要使用到联系人数据库中的 * raw_contacts表: * contact_id:联系人id * d ...

随机推荐

  1. SGU 248. Integer Linear Programming( 背包dp )

    看了半天...发现就是个背包...然后就不打算敲了. 看了一眼forum..顿时吓傻..其他人用了gcd啊什么的各种奇怪的东西..然后还是敲了个背包结果就AC了= =既然写了代码就扔上来吧... -- ...

  2. Apache与Nginx优缺点比较

    本文来源:收集.整理自互联网 1.nginx相对于apache的优点:  轻量级,同样起web 服务,比apache 占用更少的内存及资源  抗并发,nginx 处理请求是异步非阻塞的,而apache ...

  3. eclipse run on server 浏览器启动设置

  4. Codeigniter使用phpexcel

    1. 去 http://phpexcel.codeplex.com/ 下载phpexcel源码,解压缩后把phpexcel/Classes里的PHPExcel文件夹和PHPExcel.php复制到CI ...

  5. Java处理java.util.ConcurrentModificationException异常

    代码: public static void reduce(HashMap<String, Integer> hashMap, final Integer count) { Iterato ...

  6. APM代码学习笔记1

    libraries目录 传感器 AP_InertialSensor 惯性导航传感器 就是陀螺仪加速计 AP_Baro 气压计 居然支持BMP085 在我印象中APM一直用高端的MS5611 AP_Co ...

  7. 重拾javascript动态客户端网页脚本

    笔记一: 一.DOM 作用: ·              DOM(Doument Object Model) 1.document文档 HTML 文件 (标记语言) <html> < ...

  8. Python输出中文乱码问题

    //建立一个文件test.txt,文件格式用ANSI,内容为: //abc中文 //用python来读取 # coding=gbk print open("Test.txt").r ...

  9. android的快速开发框架集合

    出自:http://blog.csdn.net/shulianghan/article/details/18046021 1.Afinal  (快速开发框架) 简介:http://www.oschin ...

  10. Java关键字static

    链接地址:http://www.cnblogs.com/devinzhang/archive/2011/12/13/2286367.html static表示“全局”或者“静态”的意思,用来修饰成员变 ...