java - Onclick listner to expandable listview -
i have created expandable listview, onclick listener child list items not attached.
the activity code:
public class myactivity extends activity { private expandablelistview mexpandablelist; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mexpandablelist = (expandablelistview)findviewbyid(r.id.expandable_list); arraylist<parent> arrayparents = new arraylist<parent>(); arraylist<string> arraychildren = new arraylist<string>(); //here set parents , children (int = 0; < 2; i++){ //for each "i" create new parent object set title , children parent parent = new parent(); parent.settitle("parent " + i); arraychildren = new arraylist<string>(); (int j = 0; j < 3; j++) { arraychildren.add("child " + j); } parent.setarraychildren(arraychildren); //in array add parent object. use arrayparents @ setadapter arrayparents.add(parent); } //sets adapter provides data list. mexpandablelist.setadapter(new mycustomadapter(myactivity.this,arrayparents)); } }
the custom adapter lists:
public class mycustomadapter extends baseexpandablelistadapter { private layoutinflater inflater; private arraylist<parent> mparent; public mycustomadapter(context context, arraylist<parent> parent){ mparent = parent; inflater = layoutinflater.from(context); } @override //counts number of group/parent items list knows how many times calls getgroupview() method public int getgroupcount() { return mparent.size(); } @override //counts number of children items list knows how many times calls getchildview() method public int getchildrencount(int i) { return mparent.get(i).getarraychildren().size(); } @override //gets title of each parent/group public object getgroup(int i) { return mparent.get(i).gettitle(); } @override //gets name of each item public object getchild(int i, int i1) { return mparent.get(i).getarraychildren().get(i1); } @override public long getgroupid(int i) { return i; } @override public long getchildid(int i, int i1) { return i1; } @override public boolean hasstableids() { return true; } @override //in method must set text see parent/group on list public view getgroupview(int i, boolean b, view view, viewgroup viewgroup) { if (view == null) { view = inflater.inflate(r.layout.list_item_parent, viewgroup,false); } textview textview = (textview) view.findviewbyid(r.id.list_item_text_view); //"i" position of parent/group in list textview.settext(getgroup(i).tostring()); //return entire view return view; } @override //in method must set text see children on list public view getchildview(int i, int i1, boolean b, view view, viewgroup viewgroup) { if (view == null) { view = inflater.inflate(r.layout.list_item_child, viewgroup,false); } textview textview = (textview) view.findviewbyid(r.id.list_item_text_child); //"i" position of parent/group in list , //"i1" position of child textview.settext(mparent.get(i).getarraychildren().get(i1)); //return entire view return view; } @override public boolean ischildselectable(int i, int i1) { return true; } @override public void registerdatasetobserver(datasetobserver observer) { /* used make notifydatasetchanged() method work */ super.registerdatasetobserver(observer); } }
the parent class:
public class parent { private string mtitle; private arraylist<string> marraychildren; public string gettitle() { return mtitle; } public void settitle(string mtitle) { this.mtitle = mtitle; } public arraylist<string> getarraychildren() { return marraychildren; } public void setarraychildren(arraylist<string> marraychildren) { this.marraychildren = marraychildren; } }
what should add onclick listener child list items?
add after setadapter of expandable list
mexpandablelist.setonchildclicklistener(new onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v,int groupposition, int childposition, long id) { /* must make use of view v, find view id , extract text below*/ textview tv= (textview) v.findviewbyid(r.id.childtextview); string data= tv.gettext().tostring(); return true; // missed } });