arrays - How to populate ExpandableListView in Android at Runtime? -
array "feedback" contains keyword_id , feedback
eg.
{keyword_id= 1, feedback="test1"}, {keyword_id=1, feedback="test2"}, {keyword_id=2, feedback="test1"} so, want add every keyword_id listdataheader , feedback listdatachild. keyword_id=1 should show 2 feedbacks , keyword_id=2 should show feedback.
how can achieve that?
my current code able show keyword_id=1 , feedback="test1"
try { jsonobject jsonresponse; jsonresponse = new jsonobject(result); jsonarray jsonmainnode = jsonresponse.optjsonarray("feedbacks"); if(jsonmainnode != null) { int lengthjsonarr = jsonmainnode.length(); (int = 0; < lengthjsonarr; i++) { listdataheader = new arraylist<string>(); listdatachild = new hashmap<string, list<string>>(); jsonobject jsonchildnode = jsonmainnode.getjsonobject(i); listdataheader.add(jsonchildnode.getstring("keyword_id")); list<string> key1 = new arraylist<string>(); key1.add(jsonchildnode.getstring("feedback")); listdatachild.put(listdataheader.get(0), key1); } listadapter = new expandablelistadapter(getactivity(), listdataheader, listdatachild); explistview.setadapter(listadapter); } } catch (jsonexception e) { e.printstacktrace(); } complete adapter class
public class expandablelistadapter extends baseexpandablelistadapter { private context _context; private list<string> _listdataheader; private hashmap<string, list<string>> _listdatachild; public expandablelistadapter(context context, list<string> listdataheader, hashmap<string, list<string>> listchilddata) { this._context = context; this._listdataheader = listdataheader; this._listdatachild = listchilddata; } @override public object getchild(int groupposition, int childposititon) { return this._listdatachild.get(this._listdataheader.get(groupposition)) .get(childposititon); } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public view getchildview(int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { final string childtext = (string) getchild(groupposition, childposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.list_item, null); } textview txtlistchild = (textview) convertview .findviewbyid(r.id.lbllistitem); txtlistchild.settext(childtext); return convertview; } @override public int getchildrencount(int groupposition) { return this._listdatachild.get(this._listdataheader.get(groupposition)) .size(); } @override public object getgroup(int groupposition) { return this._listdataheader.get(groupposition); } @override public int getgroupcount() { return this._listdataheader.size(); } @override public long getgroupid(int groupposition) { return groupposition; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { string headertitle = (string) getgroup(groupposition); if (convertview == null) { layoutinflater infalinflater = (layoutinflater) this._context .getsystemservice(context.layout_inflater_service); convertview = infalinflater.inflate(r.layout.list_group, null); } textview lbllistheader = (textview) convertview .findviewbyid(r.id.lbllistheader); lbllistheader.settypeface(null, typeface.bold); lbllistheader.settext(headertitle); return convertview; } @override public boolean hasstableids() { return false; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } }
sometimes it's easier let 3rd party library heavy lifting you. suggest using nfrolodexarrayadapter. works great situations yours header of object can determined object itself. there's plenty of example code using on repo , demo app well.
your code following:
the activity
//convert jsonarray arraylist arraylist<jsonobject> listdata = new arraylist<>(); int lengthjsonarr = jsonmainnode.length(); (int = 0; < lengthjsonarr; ++i){ listdata.add(jsonmainnode.getjsonobject(i)); } //just pass in arraylist of jsonobjects. adapter handle sorting appropriate headers myexpandablelistadapter adapter = new myexpandablelistadapter(this, listdata); explistview.setadapter(adapter); the adapter
private class myexpandablelistadapter extends nfrolodexarrayadapter<string, jsonobject> { public myexpandablelistadapter(context activity, collection<jsonobject> items) { super(activity, items); } @override public date creategroupfor(jsonobject childitem) { //this how adapter determines headers , child items belong return childitem.getstring("keyword_id"); } @override public view getchildview(layoutinflater inflater, int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { //inflate view //get jsonobject data view jsonobject data = getchild(groupposition, childposition); //fill view data } @override public view getgroupview(layoutinflater inflater, int groupposition, boolean isexpanded, view convertview, viewgroup parent) { //inflate header view //gets header string view string headertitle = getgroup(groupposition); //fill view headertitle } }
Comments
Post a Comment