expandablelistview - Android how to init child and parent data for expandable listview -


greetings everyone. developing app want implement dynamic expandablelistview.

clientfunction.java

public class clientfunction extends appcompatactivity {     string id, name, mo, city, pin, date, profmail;     sessionmanager session;     arraylist<hashmap<string, string>> arraylist;     listview orderlist;     orderlistadapter adapter;     string amount, status;     textview amt;     linearlayout orderempty;     dialog dialog;     list<string> listdataheader;     hashmap<string, list<string>> listdatachild;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_client_function);         toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);         final actionbar ab = getsupportactionbar();         assert ab != null;         ab.setdisplayhomeasupenabled(true);         ab.settitle("orders");         intent = getintent();         session = new sessionmanager(this);         hashmap<string, string> user = session.getuserdetails();         profmail = user.get(sessionmanager.key_email);         orderempty = (linearlayout) findviewbyid(r.id.layout_event_empty);         orderempty.setvisibility(view.gone);         id = i.getstringextra("clientid");         name = i.getstringextra("clientname");         city = i.getstringextra("address");         mo = i.getstringextra("mobileno");         pin = i.getstringextra("pincode");         date = i.getstringextra("date");         listdataheader = new arraylist<string>();         listdatachild = new hashmap<string, list<string>>();         new httpasynctask().execute(sessionmanager.getamateurpath() + "vieworder");      }      private class httpasynctask extends asynctask<string, void, string> {         @override         protected string doinbackground(string... urls) {             return post(urls[0]);         }          @override         protected void onpostexecute(string result) {             orderlist = (listview) findviewbyid(r.id.order_list);              // pass results listviewadapter.java             adapter = new orderlistadapter(clientfunction.this, arraylist);             // set adapter listview             orderlist.setadapter(adapter);         }     }      public string post(string url) {         inputstream inputstream;         string result = "";         try {             arraylist = new arraylist<hashmap<string, string>>();              httpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost(url);              string json = "";              jsonobject jsonobject = new jsonobject();             jsonobject.accumulate("username", profmail);             jsonobject.accumulate("cid", id);              json = jsonobject.tostring();             log.d("jsonstringsend", json);              stringentity se = new stringentity(json);             httppost.setentity(se);              httppost.setheader("accept", "application/json");             httppost.setheader("content-type", "application/json");              httpresponse httpresponse = httpclient.execute(httppost);             inputstream = httpresponse.getentity().getcontent();              if (inputstream != null) {                 result = convertinputstreamtostring(inputstream);                 log.d("jsonresponce", result);                 string add = "";                 jsonarray jsonarray = new jsonarray(result);                 (int = 0; < jsonarray.length(); i++) {                     hashmap<string, string> map = new hashmap<string, string>();                     jsonobject mainjj = new jsonobject((string) jsonarray.get(i));                     map.put("item", mainjj.getstring("item"));                     map.put("total", mainjj.getstring("total"));                     map.put("orderno", mainjj.getstring("orderno"));                     map.put("quantity", mainjj.getstring("quantity"));                     map.put("extrarate", mainjj.getstring("extrarate"));                     map.put("rate", mainjj.getstring("rate"));                     map.put("detail", mainjj.getstring("detail"));                     arraylist.add(map);                     listdataheader.add(mainjj.getstring("item"));                      list<string> mainjj.getstring("item") = new arraylist<string>();                     mainjj.getstring("item").add("audi");                     mainjj.getstring("item").add("bmw");                     mainjj.getstring("item").add("honda ");                     mainjj.getstring("item").add("polo");                  }                 log.d("arraylist", arraylist.tostring());                 log.d("arraylist123456789", "" + listdataheader.tostring());              }           } catch (exception e) {              log.d("inputstream", e.getlocalizedmessage());         }         return result;     }      private string convertinputstreamtostring(inputstream inputstream) throws ioexception {         bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream));         string line = "";         string result = "";         while ((line = bufferedreader.readline()) != null)             result += line;         inputstream.close();         return result;     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_client_function, menu);         //        menuitem item = menu.finditem(r.id.add_order);         //        spannablestringbuilder builder = new spannablestringbuilder("*    login");         //        // replace "*" icon         //        builder.setspan(new imagespan(this, r.drawable.edit), 0, 1, spanned.span_exclusive_exclusive);         //        item.settitle(builder);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         switch (item.getitemid()) {             case android.r.id.home: {                 finish();                 return true;             }              case r.id.profile: {                 intent ii = new intent(clientfunction.this, viewprofile.class);                 ii.putextra("clientid", id);                 startactivity(ii);                 break;              }             case r.id.add_order: {                 intent ii = new intent(clientfunction.this, addorder.class);                 ii.putextra("clientid", id);                 ii.putextra("clientname", name);                 ii.putextra("date", date);                 ii.putextra("pincode", pin);                 ii.putextra("address", city);                 ii.putextra("mobileno", mo);                 startactivity(ii);                 break;              }             adapter = new orderlistadapter(clientfunction.this, arraylist);             case r.id.add_payment: {                 dialog = new dialog(this);                 // dialog.requestwindowfeature(window.feature_no_title);                 dialog.setcontentview(r.layout.addpayment);                 dialog.settitle(html.fromhtml("<font color='#ff7f27'>add payment</font>"));                 // dialog.getwindow().setbackgrounddrawable(new colordrawable(color.transparent));                 dialog.show();                 amt = (textview) dialog.findviewbyid(r.id.amount);                 button close = (button) dialog.findviewbyid(r.id.save);                 close.setonclicklistener(new view.onclicklistener() {                     @override                     public void onclick(view v) {                         amount = amt.gettext().tostring();                         if (amount.length() != 0) {                             new httpasynctask1().execute(sessionmanager.getamateurpath() + "addprofessionalpayment");                          } else {                             amt.seterror("enter amount");                         }                     }                 });                  break;             }         }         return super.onoptionsitemselected(item);     }      private class httpasynctask1 extends asynctask<string, void, string> {         @override         protected string doinbackground(string... urls) {             return post1(urls[0]);         }          @override         protected void onpostexecute(string result) {             if (status.equalsignorecase("success")) {                 amt.sethint("enter amount");                 dialog.dismiss();                 toast.maketext(clientfunction.this, "amount added successfully", toast.length_short).show();             } else                 toast.maketext(clientfunction.this, "amount added failed", toast.length_short).show();         }     }      public string post1(string url) {         inputstream inputstream;         string result = "";         try {             arraylist = new arraylist<hashmap<string, string>>();              httpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost(url);              string json = "";              jsonobject jsonobject = new jsonobject();             jsonobject.accumulate("username", profmail);             jsonobject.accumulate("clientid", id);             jsonobject.accumulate("paymentrs", amount);               json = jsonobject.tostring();             log.d("jsonstringsend", json);              stringentity se = new stringentity(json);             httppost.setentity(se);              httppost.setheader("accept", "application/json");             httppost.setheader("content-type", "application/json");              httpresponse httpresponse = httpclient.execute(httppost);             inputstream = httpresponse.getentity().getcontent();              if (inputstream != null) {                 result = convertinputstreamtostring(inputstream);                 log.d("jsonresponce", result);                 string add = "";                 jsonobject mainjj = new jsonobject(result);                 status = mainjj.getstring("status");              }           } catch (exception e) {              log.d("inputstream", e.getlocalizedmessage());         }         return result;     }      public class orderlistadapter extends baseadapter {         context context;         layoutinflater inflater;         button send1;          arraylist<hashmap<string, string>> data;         int lastposition = 0;         private colorgenerator mcolorgenerator = colorgenerator.material;          hashmap<string, string> resultp = new hashmap<string, string>();         private displayimageoptions options;          public orderlistadapter(context context, arraylist<hashmap<string, string>> arraylist) {             this.context = context;             data = arraylist;         }          @override         public int getcount() {             if (data.size() == 0) {                 orderempty.setvisibility(view.visible);                 orderlist.setvisibility(view.gone);              }             return data.size();         }          @override         public object getitem(int position) {             return null;         }          @override         public long getitemid(int position) {             return 0;         }          @override         public view getview(final int position, view convertview, viewgroup parent) {             inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);              view itemview = inflater.inflate(r.layout.inflate_listoforder, parent, false);             resultp = data.get(position);             textview iname = (textview) itemview.findviewbyid(r.id.iname);             textview iid = (textview) itemview.findviewbyid(r.id.iid);             textview iqty = (textview) itemview.findviewbyid(r.id.iqty);             textview itotal = (textview) itemview.findviewbyid(r.id.itotal);             iname.settext("item name: " + resultp.get("item"));             iid.settext("item id: " + resultp.get("orderno"));             iqty.settext("itemquantity: " + resultp.get("quantity"));             itotal.settext("itemtotal: " + resultp.get("total"));              itemview.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     resultp = data.get(position);                     intent ii = new intent(clientfunction.this, vieworder.class);                     ii.putextra("item", resultp.get("item"));                     ii.putextra("clientid", id);                     ii.putextra("orderno", resultp.get("orderno"));                     ii.putextra("quantity", resultp.get("quantity"));                     ii.putextra("total", resultp.get("total"));                     ii.putextra("extrarate", resultp.get("extrarate"));                     ii.putextra("rate", resultp.get("rate"));                     ii.putextra("detail", resultp.get("detail"));                     startactivity(ii);                 }             });              animation animation = animationutils.loadanimation(context, (position > lastposition) ? r.anim.up_from_bottom : r.anim.down_from_top);             itemview.startanimation(animation);             lastposition = position;             return itemview;         }     }  } 

i don't know how add data in list<string> listdataheader , hashmap<string, list<string>> listdatachild.

i added data in listdataheader, don't know how add data dynamically listdatachild.

i adding data in listdatachild follows

  list<string> mainjj.getstring("item") = new arraylist<string>();                 mainjj.getstring("item").add("audi");                 mainjj.getstring("item").add("bmw");                 mainjj.getstring("item").add("honda ");                 mainjj.getstring("item").add("polo"); 

but isn't working. please me solution. can proceed coding. in advance.

your code quit confusing, here tell how add data (static example) both expandablelistview header , child.

//function return map add in adapter  public static map<string, list<string>> getdata()  {    map<string, list<string>> expandablelist = new treemap<>();     list<string> child1 = new arraylist<string>();    child1.add("child data......");     list<string> child2 = new arraylist<string>();    child2.add("child data......");     list<string> child3 = new arraylist<string>();    child3.add("child data........");      expandablelist.put("header-1", child1);    expandablelistdetail.put("header-2",child2);    expandablelistdetail.put("header-3",child3);    expandablelistdetail.put("header-4",child4);     return expandablelistdetail; } 

here adding own data , dynamic behaviour have add data using loop "expandabllist" & "child" list.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -