zoukankan      html  css  js  c++  java
  • android 支持分组和联系人展示的一个小样例

    先看效果图:




    要实现这个效果,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下载

  • 相关阅读:
    MySQL数据类型与操作
    MySQL 初识
    python中的线程
    python中的进程
    python中基于tcp协议与udp的通信(数据传输)
    字符串str.format()方法的个人整理
    进度条打印函数
    套接字错误搜集
    正则表达式 整理(w s d 点 贪婪匹配 非贪婪匹配 * + ? {} | [] ^ $  单词边界 分组、re.findall()、re.split()、re.search()、re.match()、re.compile()、re.sub())
    软件开发架构介绍||OSI七层协议之物理层、数据链路层、网络层、传输层(mac地址、ip协议、断开协议、tcp协议之三次握手四次挥手)
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5182353.html
Copyright © 2011-2022 走看看