A small example of android's support for group and contact display

Source: Internet
Author: User

A small example of android's support for group and contact display

First look:




To achieve this effect, the activity must implement ExpandableListActivity.

@ Overridepublic 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); // avoid black getExpandableListView () when dragging (). setDivider (null); // remove the black line (split line) under each item // custom drop-down icon getExpandableListView (). setGroupIndicator (getResources (). getDrawable (R. drawable. expander_ic_folder); configure () ;}/ *** set the adapter of ExpandableListView */private void setadat#expandablelistview () {Cursor groupCursor = mContactDataBase. getAllGroups (); // This is to query all Util groups from the database. d (TAG, "groupCursor =" + groupCursor); // the lifecycle of curosr will be related to activity startManagingCursor (groupCursor); // set my adapterContactTreeAdapterContactTreeAdapter = new ContactTreeAdapter (groupCursor, this, true, mContactDataBase); setListAdapter (contactTreeAdapter );}

Main Implementation ContactTreeAdapter

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 table field index private static final int INDEX_NAME = 1; private static final int INDEX_PHONENUMBER = 2; // group table field index 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} @ Overrideprotected Cursor getChildrenCursor (Cursor groupCursor) {// TODO Auto-generated method stubString groupName = groupCursor. getString (INDEX_GROUPNAME); // obtain the current group name Cursor childCursor = mContactDataBase. getContactsByGroupName (groupName); return childCursor;} @ Overrideprotected View newGroupView (Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {// TODO Auto-generated method stubUtil. d (TAG, "newGroupView"); LayoutInflater inflate = LayoutInflater. from (mContext); View view = inflate. inflate (R. layout. grouplayout, null); bindGroupView (view, context, cursor, isExpanded); return view ;}@ Overrideprotected void bindGroupView (View view, Context context, Cursor cursor, boolean isExpanded) {// TODO Auto-generated method stubUtil. 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 + "]") ;}@ Overrideprotected View newChildView (Context context, Cursor cursor, boolean isLastChild, ViewGroup parent) {// TODO Auto-generated method stubUtil. d (TAG, "newChildView"); LayoutInflater inflate = LayoutInflater. from (mContext); View view = inflate. inflate (R. layout. childlayout, null); bindChildView (view, context, cursor, isLastChild); return view ;}@ Overrideprotected void bindChildView (View view, Context context, Cursor cursor, boolean isLastChild) {// TODO Auto-generated method stubUtil. 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 ));}}

This is because the function name of this adapter can tell what it is, and I will not explain it one by one.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.