Saturday 15 September 2012

java - Why is custom ExpandableListView not displayed in android -



java - Why is custom ExpandableListView not displayed in android -

i trying create custom expandablelistview having kid or node. parents don't have children. don't want show anything. in other words

i need display

parent 1 parent 2 child1 child2 child3 parent 3 parent 4 child1

so seek create xml route_dashboard.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <expandablelistview android:id="@+id/expandablelistview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" > </expandablelistview> </relativelayout>

custom adapter

public class customexpendablelistview extends baseexpandablelistadapter { private string[] fathername={"naveen","ravi","sharma","uncle","rahat"}; string[] ravichildrenname={"abc","pqr","mnn"}; string[] sharmachildrenname={"zxa","yh","er"}; context context; public customexpendablelistview(context context) { this.context=context; } @override public int getgroupcount() { homecoming 0; } @override public int getchildrencount(int groupposition) { homecoming 0; } @override public object getgroup(int groupposition) { homecoming null; } @override public object getchild(int groupposition, int childposition) { homecoming null; } @override public long getgroupid(int groupposition) { homecoming 0; } @override public long getchildid(int groupposition, int childposition) { homecoming 0; } @override public boolean hasstableids() { homecoming false; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { layoutinflater minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); if (convertview == null) { convertview = minflater.inflate(r.layout.parenttextview, null); } textview item = (textview) convertview.findviewbyid(r.id.parenttextview); item.settext(fathername[groupposition]); homecoming item; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { homecoming null; } @override public boolean ischildselectable(int groupposition, int childposition) { // todo auto-generated method stub homecoming false; } }

call main function

@override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.route_dashboard); expendablelistview = (expandablelistview) findviewbyid(r.id.expandablelistview1); adapter = new customexpendablelistview(this); expendablelistview.setadapter(adapter); }

child.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/childtextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:text="medium text" android:textappearance="?android:attr/textappearancemedium" /> </relativelayout>

parenttextviewxml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/parenttextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:text="medium text" android:textappearance="?android:attr/textappearancemedium" /> </relativelayout>

i take 3 array

private string[] fathername={"naveen","ravi","sharma","uncle","rahat"}; string[] ravichildrenname={"abc","pqr","mnn"}; string[] sharmachildrenname={"zxa","yh","er"};

fathername parents nodes.ravichildrenname kid of ravi parent node .sharmachildrenname kid of sharma

could please give code.?

your getgroupcount method must homecoming value greater zero. in case list view display something that:

@override public int getgroupcount() { homecoming fathername.length; } @override public int getchildrencount(int groupposition) { if (fathername[groupposition].equalsignorecase("ravi")) { homecoming ravichildrenname.length; } if (fathername[groupposition].equalsignorecase("sharma")) { homecoming sharmachildrenname.length; } homecoming 0; }

you need implement getchildview method listview:

@override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { view convertview, viewgroup parent) { layoutinflater minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); if (convertview == null) { convertview = minflater.inflate(r.layout.childview, null); } textview item = (textview) convertview.findviewbyid(r.id.childtextview); if (fathername[groupposition].equalsignorecase("ravi")) { item.settext(ravichildrenname[childposition]); } if (fathername[groupposition].equalsignorecase("sharma")) { item.settext( sharmachildrenname[childposition]); } homecoming item; }

java android android-listview expandablelistview

No comments:

Post a Comment