先看效果图:

要实现这个效果,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. 正整数从1到N,输出按照字典序排序的前K个数

    #include <iostream> #include <cassert> using namespace std; ; char a[max_len]; void topK ...

  2. BZOJ 1969: [Ahoi2005]LANE 航线规划( 树链剖分 )

    首先我们要时光倒流, 倒着做, 变成加边操作维护关键边. 先随意搞出一颗树, 树上每条边都是关键边(因为是树, 去掉就不连通了)....然后加边(u, v)时, 路径(u, v)上的所有边都变成非关键 ...

  3. WCF 接收、发送数据的大小及时间的设置

    <system.serviceModel> <bindings> <basicHttpBinding> <binding name="/> & ...

  4. R与数据分析旧笔记(四)画地图练习

    > library(maps) > library(geosphere) 载入需要的程辑包:sp > map("state")#画美国地图 > map(&q ...

  5. Java之工厂模式

    interface Fruit {     void eat(); } class Apple implements Fruit {     public void eat() {         S ...

  6. ThinkPHP第二十四天(JQuery常用方法、TP自动验证)

    ---恢复内容开始--- 1.JQuery常用方法 A:JS中可以用json格式数据当做数组使用,如var validate={username:false,pwd:false,pwded:false ...

  7. Open开发平台,认证,授权,计费

    1.申请appid和appkeyhttp://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0 appid:应用的唯一 ...

  8. Asp.Net Core WebApi学习笔记(四)-- Middleware

    Asp.Net Core WebApi学习笔记(四)-- Middleware 本文记录了Asp.Net管道模型和Asp.Net Core的Middleware模型的对比,并在上一篇的基础上增加Mid ...

  9. 7_Table Views

    7 // // ViewController.swift // Table Views // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. ...

  10. HelloX项目github协同开发指南

    概述 为了提高协同开发效率,HelloX项目已托管到github网站上.根据目前的开发进展,创建了下列几个子项目: HelloX操作系统内核项目:https://github.com/hellox-p ...