java - ExpandableListView doesn't appear in the right order -


i wriring android app, , decided use expandable list view. have need, list working fine, when populate it, groups appear in different order should, here's example: unexpanded

expanded

as can see, child values fine, parent values appear in random order. here's full code have:

my fragment code:

public class buildingsfragment extends fragment {   public buildingsfragment() {     // required empty public constructor }   public static buildingsfragment newinstance(context context) {     buildingsfragment fragment = new buildingsfragment();      return fragment; }   @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);   }   @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     // inflate layout fragment     view view = inflater.inflate(r.layout.fragment_buildings, container, false);       final expandablelistview expandablelistview = (expandablelistview) view.findviewbyid(r.id.expandablelistviewbuildings);     hashmap<string, list<string>> expandablelistdetail = expandablelistdatapump.getdata();     list<string> expandablelisttitle = new arraylist<string>(expandablelistdetail.keyset());     expandablelistadapter expandablelistadapter = new customexpandablelistadapter(getcontext(), expandablelisttitle, expandablelistdetail);     expandablelistview.setadapter(expandablelistadapter);       expandablelistview.setonchildclicklistener(new expandablelistview.onchildclicklistener() {         @override         public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) {               fragment fragment = null;             class fragmentclass = buildingdetailsfragment.class;                try {                 fragment = (fragment) fragmentclass.newinstance();             } catch (exception e) {                 e.printstacktrace();             }              fragmenttransaction trans = getfragmentmanager().begintransaction();               bundle args = new bundle();             args.putstring("building_name", "test");             fragment.setarguments(args);               trans.replace(r.id.framelayoutforfragments, fragment);               trans.addtobackstack(null);               trans.commit();               return false;         }     });       return view; } } 

my custom adapter:

public class customexpandablelistadapter extends baseexpandablelistadapter {  private context context; private list<string> expandablelisttitle; private hashmap<string, list<string>> expandablelistdetail;  public customexpandablelistadapter(context context, list<string> expandablelisttitle, hashmap<string, list<string>> expandablelistdetail) {     this.context = context;     this.expandablelisttitle = expandablelisttitle;     this.expandablelistdetail = expandablelistdetail; }  @override public object getchild(int listposition, int expandedlistposition) {     return this.expandablelistdetail.get(this.expandablelisttitle.get(listposition))             .get(expandedlistposition); }  @override public long getchildid(int listposition, int expandedlistposition) {     return expandedlistposition; }  @override public view getchildview(int listposition, final int expandedlistposition,                          boolean islastchild, view convertview, viewgroup parent) {     final string expandedlisttext = (string) getchild(listposition, expandedlistposition);     if (convertview == null) {         layoutinflater layoutinflater = (layoutinflater) this.context                 .getsystemservice(context.layout_inflater_service);         convertview = layoutinflater.inflate(r.layout.list_view_buildings_row_layout, null);     }     textview expandedlisttextview = (textview) convertview             .findviewbyid(r.id.expandedlistitem);     expandedlisttextview.settext(expandedlisttext);     return convertview; }  @override public int getchildrencount(int listposition) {     return this.expandablelistdetail.get(this.expandablelisttitle.get(listposition))             .size(); }  @override public object getgroup(int listposition) {     return this.expandablelisttitle.get(listposition); }  @override public int getgroupcount() {     return this.expandablelisttitle.size(); }  @override public long getgroupid(int listposition) {     return listposition; }  @override public view getgroupview(int listposition, boolean isexpanded,                          view convertview, viewgroup parent) {     string listtitle = (string) getgroup(listposition);     if (convertview == null) {         layoutinflater layoutinflater = (layoutinflater) this.context.                 getsystemservice(context.layout_inflater_service);         convertview = layoutinflater.inflate(r.layout.list_view_buildings_group_layout, null);     }     textview listtitletextview = (textview) convertview             .findviewbyid(r.id.listgroup);     listtitletextview.settypeface(null, typeface.bold);     listtitletextview.settext(listtitle);     return convertview; }  @override public boolean hasstableids() {     return false; }  @override public boolean ischildselectable(int listposition, int expandedlistposition) {     return true; } } 

the expandablelistdatapump using:

public class expandablelistdatapump { public static hashmap<string, list<string>> getdata() {      hashmap<string, list<string>> expandablelistdetail = new hashmap<string, list<string>>();       list<string> abuildings = new arraylist<string>();     abuildings.add("a-1");     abuildings.add("a-2");     abuildings.add("a-3");     abuildings.add("a-5");     abuildings.add("a-7");     expandablelistdetail.put("a", abuildings);      list<string> bbuildings = new arraylist<string>();     bbuildings.add("b-1");     bbuildings.add("b-2");     bbuildings.add("b-3");     bbuildings.add("b-5");     bbuildings.add("b-6");     expandablelistdetail.put("b", bbuildings);      list<string> cbuildings = new arraylist<string>();     cbuildings.add("c-1");     cbuildings.add("c-3");     cbuildings.add("c-4");     cbuildings.add("c-5");     cbuildings.add("c-6");     expandablelistdetail.put("c", cbuildings);      list<string> dbuildings = new arraylist<string>();     dbuildings.add("d-1");     dbuildings.add("d-2");     dbuildings.add("d-3");     dbuildings.add("d-5");     dbuildings.add("d-7");     expandablelistdetail.put("d", dbuildings);      return expandablelistdetail; } } 

i think that's related expandablelistview. leave hashmap, if there no other way, can change treemap , use it's sorting function (as last resort)? help.

p.s. while doing expandablelistview following tutorial (i don't know if matters): tutorial

according java specs hashmap, order not guaranteed:

...this class makes no guarantees order of map; in particular, not guarantee order remain constant on time...

so, if want guarantee order of groups, you'll have use different data structure.

see this answer similar discussion, , suggested usage of linkedhashmap, maintains insertion order.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -