先看效果图:

要实现这个效果,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. 移动前端制作篇之javascript篇

    javascript(简称js)语言在移动前端应用很广.可以说必不可少,许多效果都是和js相关的.包括现在移动端的一些框架.jqmobi.jqtouch.sencha touch.jquerymobi ...

  2. Windows系统下远程Linux系统

    Windows系统下远程Linux系统 工具:Xmanager 启动界面: 配置保存路径(win7): C:\Users\Administrator\AppData\Roaming\NetSarang ...

  3. Cortex-M3 动态加载二(RWPI数据无关实现)

    上一篇关于动态加载讲述的是M3下面的ropi的实现细节,这一篇则讲述RW段的实现细节以及系统加载RW段的思路,我在M3上根据这个思路可以实现elf的动态加载,当然进一步的可以优化很多东西,还可以研究将 ...

  4. 窗口嵌入到另一个窗口(VC和QT都有)

    1.用vc新建一个dialog1工程.属性默认. 2.insert一个dialog2,改为child. 3.在dialog1中包含dialog2头文件,在一个按钮事件中显示dialog2: Cdial ...

  5. 源码安装rsyslog

    <pre name="code" class="html">下载下列软件 json-c-0.12-20140410.tar.gz---------- ...

  6. VS2010/MFC对话框:颜色对话框

    颜色对话框 在上一节中为大家讲解了字体对话框的使用方法,熟悉了字体对话框,本节继续讲另一种通用对话框--颜色对话框. 颜色对话框大家肯定也不陌生,我们可以打开它选择需要的颜色,简单说,它的作用就是用来 ...

  7. ubuntu安装XHProf

    1. 安装XHProf wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2 s ...

  8. php在apache中运行模式

    php在apache中运行模式 (2011-12-18 02:38:27) 标签: 杂谈 分类: 服务器及软件 一.php在php在三种工作方式:Apache 模块DLL) 以下分别比较: 1. ph ...

  9. 视频主观质量评价工具:MSU Perceptual Video Quality tool

    MSU Perceptual Video Quality tool是莫斯科国立大学(Moscow State University)的Graphics and Media Lab制作的一款视频主观评价 ...

  10. linux下用mail发送邮件

    利用外部邮箱发送邮件的方法 bin/mail会默认使用本地sendmail发送邮件,这样要求本地的机器必须安装和启动Sendmail服务,配置很麻烦,并且会带来不必要的 资源占用.而通过改动配置文件能 ...