A small example of Android support grouping and contact presentation

Source: Internet
Author: User

Look first:




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);//Drag to avoid black Getexpandablelistview (). Setdivider (null);//Remove each of the following black lines (split line)// Custom drop-down icon Getexpandablelistview (). Setgroupindicator (Getresources (). getdrawable (R.drawable.expander_ic_folder)); Setadatperforexpandablelistview ();} /** * Set Expandablelistview adapter */private void Setadatperforexpandablelistview () {Cursor groupcursor =  Mcontactdatabase.getallgroups (); This is from the database to query out all the group UTIL.D (TAG, "groupcursor=" + groupcursor);//CUROSR life cycle will be related to activity startmanagingcursor ( Groupcursor);//Set my adapter<strong>contacttreeadapter </strong>contacttreeadapter = New Contacttreeadapter (Groupcursor, this, true, mcontactdatabase); Setlistadapter (Contacttreeadapter);} 

Main implementationContacttreeadapter this adapter .

public class Contacttreeadapter extends Cursortreeadapter {/** log tag. */private static final String tag = "Contacttreead Apter ";/** 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 privat e static final int index_groupname = 1;public contacttreeadapter (cursor cursor, Context context,boolean autorequery, Conta Ctdatabase 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);// Get 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-generat Ed 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-genera Ted 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);d escription.settextkeepstate ( Cursor.getstring (Index_phonenumber));}} <strong></strong>

Because this adapter function is named to see what it is that does not explain

Code can be downloaded in http://download.csdn.net/detail/baidu_nod/7684649

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.