java - How to add different image for each group in expandable Listview -
in expandable listview, have 3 groups. want add different 3 image group. tried many codes, not working.
my code:
main activity:
hashmap<string, list<string>> rightdrawerlistdetail = getdata(); list<string> rightdrawerlisttitle = new arraylist<string>(rightdrawerlistdetail.keyset()); adapterr = new customexpandablelistadapter(this, rightdrawerlisttitle,rightdrawerlistdetail); mrightdrawerlist.setadapter(adapterr); private hashmap<string, list<string>> getdata(){ hashmap<string, list<string>> expandablelistdetail = new hashmap<string, list<string>>(); arraylist<string> arraylist = mydb.getalladdress(); list asrider = new arraylist(); asrider.add("hello"); list asridee = new arraylist(); asridee.add("hai"); list recent = new arraylist(); recent.add("success"); expandablelistdetail.put("as rider",asrider); expandablelistdetail.put("as ridee",asridee); expandablelistdetail.put("my recent activities",recent); return expandablelistdetail; }
customexpandablelistadapter.java:
public class customexpandablelistadapter extends baseexpandablelistadapter { private context mcontext; private list<string> mgroups; private layoutinflater minflater; private hashmap<string, list<string>> mexpandablelistdetail; public customexpandablelistadapter(context context, list<string> groups,hashmap<string, list<string>> expandablelistdetail) { mcontext = context; mgroups = groups; mexpandablelistdetail = expandablelistdetail; minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } @override public int getgroupcount() { return mgroups.size(); } @override public int getchildrencount(int groupposition) { return mexpandablelistdetail.get(mgroups.get(groupposition)).size(); } @override public object getgroup(int groupposition) { return mgroups.get(groupposition); } @override public object getchild(int groupposition, int childposition) { return mexpandablelistdetail.get(mgroups.get(groupposition)).get(childposition); } @override public long getgroupid(int groupposition) { return 0; } @override public long getchildid(int groupposition, int childposition) { return 0; } @override public boolean hasstableids() { return false; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { if (convertview == null) { convertview = minflater.inflate(r.layout.right_drawer_group, null); } // group item string listtitle = (string) getgroup(groupposition); // set group name textview textview = (textview) convertview.findviewbyid(r.id.textgroup); textview.settext(listtitle); imageview indicator = (imageview) convertview.findviewbyid(r.id.groupindicator); if (isexpanded) { indicator.setimageresource(r.drawable.arrowup); } else { indicator.setimageresource(r.drawable.arrowdown); } return convertview; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { if (convertview == null) { convertview = minflater.inflate(r.layout.right_drawer_child, null); } // child name string children = (string) getchild(groupposition, childposition); // set child name textview text = (textview) convertview.findviewbyid(r.id.textchild); text.settext(children); /*convertview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mcontext, children, toast.length_short).show(); } });*/ return convertview; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; }
}
please me!
thanks in advance
in right_drawer_group.xml add imageview want group image placed, lets groupimage.
in getgroupview:
@override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { if (convertview == null) { convertview = minflater.inflate(r.layout.right_drawer_group, null); } //set group image imageview groupimage = (imageview)convertview.findviewbyid(r.id.groupimage); switch((string)getgroup(groupposition)) { case "as rider": //groupimage set correct image break; case "as ridee": //groupimage set correct image break; case "my recent activities": //groupimage set correct image break; } // group item string listtitle = (string) getgroup(groupposition); // set group name textview textview = (textview) convertview.findviewbyid(r.id.textgroup); textview.settext(listtitle); imageview indicator = (imageview) convertview.findviewbyid(r.id.groupindicator); if (isexpanded) { indicator.setimageresource(r.drawable.arrowup); } else { indicator.setimageresource(r.drawable.arrowdown); } return convertview; }
it's not neat solution if change group name break, work now. better solution pass groups objects containing getname , getimageid.
Comments
Post a Comment