java - how to make ExpandableListView with JSON array -


i want make expandablelistview, gets data response server(json array) groups , children. json :

  [     {         "         "begdate": "23/07/2013",         "nama": "optik internasional",         "img_id": 2,         "alamat": "jl. nasional no 23 meulaboh",         "enddate": "23/07/2015",     },     {          "begdate": "01/05/2013",         "nama": "el john smoking lounge",          "img_id": 3,         "alamat": "bandara sultan iskandar muda, banda aceh",         "enddate": "30/04/2014",     },     {         "begdate": "09/11/2012",         "nama": "espresso coffe",         "img_id": 2         "alamat": "jl. soekarno hatta no. 16-17, simpang dodi",         "enddate": "09/11/2014",     },     {          "begdate": "16/03/2012",         "nama": "luzi perfume",          "img_id": 3,         "alamat": "jl. mata ie ketapang ii",         "enddate": "16/03/2014",     }     ] 

"alamat" , nama" become groups, "begdate" , "enddate" become children. have follow tutorial here : http://en.wikicode.org/index.php/custom_expandablelistview when implement it, got error there wrong code? logcat :

    08-16 15:18:34.541: e/androidruntime(416): fatal exception: main 08-16 15:18:34.541: e/androidruntime(416): java.lang.indexoutofboundsexception: invalid index 1, size 0 08-16 15:18:34.541: e/androidruntime(416):  @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:257) 08-16 15:18:34.541: e/androidruntime(416):  @ java.util.arraylist.get(arraylist.java:311) 08-16 15:18:34.541: e/androidruntime(416):  @ com.example.belajaraccordion.terbarusimascard.fetchresponse(terbarusimascard.java:106) 08-16 15:18:34.541: e/androidruntime(416):  @ com.example.belajaraccordion.terbarusimascard.access$1(terbarusimascard.java:85) 08-16 15:18:34.541: e/androidruntime(416):  @ com.example.belajaraccordion.terbarusimascard$terbaruasynctask.onpostexecute(terbarusimascard.java:77) 08-16 15:18:34.541: e/androidruntime(416):  @ com.example.belajaraccordion.terbarusimascard$terbaruasynctask.onpostexecute(terbarusimascard.java:1) 08-16 15:18:34.541: e/androidruntime(416):  @ android.os.asynctask.finish(asynctask.java:417) 08-16 15:18:34.541: e/androidruntime(416):  @ android.os.asynctask.access$300(asynctask.java:127) 08-16 15:18:34.541: e/androidruntime(416):  @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:429) 08-16 15:18:34.541: e/androidruntime(416):  @ android.os.handler.dispatchmessage(handler.java:99) 08-16 15:18:34.541: e/androidruntime(416):  @ android.os.looper.loop(looper.java:123) 08-16 15:18:34.541: e/androidruntime(416):  @ android.app.activitythread.main(activitythread.java:3683) 08-16 15:18:34.541: e/androidruntime(416):  @ java.lang.reflect.method.invokenative(native method) 08-16 15:18:34.541: e/androidruntime(416):  @ java.lang.reflect.method.invoke(method.java:507) 08-16 15:18:34.541: e/androidruntime(416):  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 08-16 15:18:34.541: e/androidruntime(416):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 08-16 15:18:34.541: e/androidruntime(416):  @ dalvik.system.nativestart.main(native method) 

this code :

terbarusimascard.java

public class terbarusimascard extends activity {      private progressdialog dialog;     private arraylist<terbarumodel>listterbaru;     private arraylist<arraylist<arraylist<childterbaru>>> listchildterbaru;     listview list;     string phone1, begdate1, enddate1,img_id1;      @override      protected void oncreate(bundle savedinstancestate) {              super.oncreate(savedinstancestate);             setcontentview(r.layout.terbarusimascard);             listterbaru= new arraylist<terbarumodel>();             listchildterbaru= new arraylist<arraylist<arraylist<childterbaru>>>();             new terbaruasynctask().execute();      }     public class terbaruasynctask extends asynctask<void, void, string> {         string url = ("http://www.domain.com/123");          public terbaruasynctask() {             this.url=url;         }          protected void onpreexecute (){              super.onpreexecute();             dialog = progressdialog.show(terbarusimascard.this,"", "melakukan pengambilan data...");         }         @override         protected string doinbackground(void... params) {             string result = "";             try {                 result= connection.get(url);             } catch (exception e){                 result = "";                 log.d("test", e.getmessage());             }             return result;         }      @override     protected void onpostexecute (string result){         super.onpostexecute(result);         fetchresponse(result.replace("\n","").trim());         dialog.dismiss();      }       }  private void fetchresponse (string result){     if (!result.equals("")){         try {             jsonarray jsonarray = new jsonarray(result);             terbarumodel lt=null;             childterbaru ct=null;             (int i= 0; < jsonarray.length(); i++) {                 jsonobject jsonobject= jsonarray.getjsonobject (i);                  lt= new terbarumodel (                         jsonobject.optstring("img_id"),                         jsonobject.optstring("nama"),                         jsonobject.optstring("alamat")                         );                 listterbaru.add(lt);                  (int j= 0; j <jsonarray.length(); j++) {                     ct= new childterbaru (                     jsonobject.optstring("begdate"),                     jsonobject.optstring("enddate")                     );                     listchildterbaru.get(i).get(j).add(ct);                  }                 expandablelistview list = (expandablelistview) findviewbyid(r.id.expandablelistview01);                  terbaruadapter adapter = new terbaruadapter(this, listterbaru,listchildterbaru);                 list.setadapter(adapter);              }             } catch (jsonexception e){                 e.printstacktrace();             }             }         }     } 

this terbaruadapter.java :

public class terbaruadapter extends baseexpandablelistadapter{     context context;     arraylist<terbarumodel>listterbaru;     arraylist<arraylist<arraylist<childterbaru>>> listchildterbaru;     int count;      public terbaruadapter (context context, arraylist<terbarumodel>listterbaru,arraylist<arraylist<arraylist<childterbaru>>> listchildterbaru){         this.context=context;         this.listterbaru=listterbaru;         this.listchildterbaru=listchildterbaru; //      this.count=listterbaru.size(); //      this.count=listchildterbaru.size();     }     @override     public boolean areallitemsenabled()     {         return true;     }       @override     public arraylist<childterbaru> getchild(int groupposition, int childposition) {         return listchildterbaru.get(groupposition).get(childposition);     }      @override     public long getchildid(int groupposition, int childposition) {         return childposition;     }      @override     public view getchildview(int groupposition, int childposition, boolean islastchild,view convertview, viewgroup parent) {          childterbaru listchildterbaru = (childterbaru) ((arraylist<childterbaru>)getchild(groupposition, childposition)).get(0);         viewholder holder= null;          if (convertview == null) {             layoutinflater infalinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);             convertview = infalinflater.inflate(r.layout.expandablelistview_child, null);              holder=new viewholder();             holder.begdate1=(textview)convertview.findviewbyid(r.id.beg_date);             holder.enddate1=(textview)convertview.findviewbyid(r.id.end_date);         }         else{             holder=(viewholder)convertview.gettag();         }          holder.begdate1.settext(listchildterbaru.getbegdate());         holder.enddate1.settext(listchildterbaru.getenddate());          return convertview;     }     @override     public int getchildrencount(int groupposition) {         return listchildterbaru.get(groupposition).size();     }      @override     public terbarumodel getgroup(int groupposition) {         return listterbaru.get(groupposition);     }      @override     public int getgroupcount() {         return listterbaru.size();     }      @override     public long getgroupid(int groupposition) {         return groupposition;     }      @override     public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) {          terbarumodel listterbaru = (terbarumodel) getgroup(groupposition);         viewholder holder= null;         if (convertview == null) {             layoutinflater infalinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);             convertview = infalinflater.inflate(r.layout.expandablelistview_group, null);              holder=new viewholder();             holder.nama=(textview)convertview.findviewbyid(r.id.name);             holder.alamat=(textview)convertview.findviewbyid(r.id.adress);         }           else{             holder=(viewholder)convertview.gettag();          }          holder.nama.settext(listterbaru.getnama());         holder.alamat.settext(listterbaru.getalamat());           return convertview;     }      @override     public boolean hasstableids() {         return true;     }      @override     public boolean ischildselectable(int arg0, int arg1) {         return true;     }       static class viewholder{         textview begdate1, enddate1,nama, alamat, imageid;     }  } 

i have no idea where's wrong, because wrong in parsing json bellow?

for (int i= 0; < jsonarray.length(); i++) {             jsonobject jsonobject= jsonarray.getjsonobject (i);              lt= new terbarumodel (                     jsonobject.optstring("img_id"),                     jsonobject.optstring("nama"),                     jsonobject.optstring("alamat")                     );             listterbaru.add(lt);              (int j= 0; j <jsonarray.length(); j++) {                 ct= new childterbaru (                 jsonobject.optstring("begdate"),                 jsonobject.optstring("enddate")                 );                 listchildterbaru.get(i).get(j).add(ct);              } 

or there wrong else? hope can tell me where's fault... thank you

modify adapter

 public class terbaruadapter extends baseexpandablelistadapter{     context context;     arraylist<terbarumodel>listterbaru;     arraylist<arraylist<childterbaru>> listchildterbaru;     int count;      public terbaruadapter (context context, arraylist<terbarumodel>listterbaru,arraylist<arraylist<childterbaru>> listchildterbaru){         this.context=context;         this.listterbaru=listterbaru;         this.listchildterbaru=listchildterbaru; //      this.count=listterbaru.size(); //      this.count=listchildterbaru.size();     }     @override     public boolean areallitemsenabled()     {         return true;     }       @override     public childterbaru getchild(int groupposition, int childposition) {         return listchildterbaru.get(groupposition).get(childposition);     }      @override     public long getchildid(int groupposition, int childposition) {         return childposition;     }      @override     public view getchildview(int groupposition, int childposition, boolean islastchild,view convertview, viewgroup parent) {          childterbaru childterbaru = getchild(groupposition, childposition);         viewholder holder= null;          if (convertview == null) {             layoutinflater infalinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);             convertview = infalinflater.inflate(r.layout.expandablelistview_child, null);              holder=new viewholder();             holder.begdate1=(textview)convertview.findviewbyid(r.id.beg_date);             holder.enddate1=(textview)convertview.findviewbyid(r.id.end_date);             convertview.settag(holder);          }         else{             holder=(viewholder)convertview.gettag();         }          holder.begdate1.settext(childterbaru.getbegdate());         holder.enddate1.settext(childterbaru.getenddate());          return convertview;     }     @override     public int getchildrencount(int groupposition) {         return listchildterbaru.get(groupposition).size();     }      @override     public terbarumodel getgroup(int groupposition) {         return listterbaru.get(groupposition);     }      @override     public int getgroupcount() {         return listterbaru.size();     }      @override     public long getgroupid(int groupposition) {         return groupposition;     }      @override     public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) {          terbarumodel terbarumodel = (terbarumodel) getgroup(groupposition);         viewholder holder= null;         if (convertview == null) {             layoutinflater infalinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);             convertview = infalinflater.inflate(r.layout.expandablelistview_group, null);              holder=new viewholder();             holder.nama=(textview)convertview.findviewbyid(r.id.name);             holder.alamat=(textview)convertview.findviewbyid(r.id.address);             convertview.settag(holder);          }           else{             holder=(viewholder)convertview.gettag();          }          holder.nama.settext(terbarumodel.getnama());         holder.alamat.settext(terbarumodel.getalamat());           return convertview;     }      @override     public boolean hasstableids() {         return true;     }      @override     public boolean ischildselectable(int arg0, int arg1) {         return true;     }       static class viewholder{         textview begdate1, enddate1,nama, alamat, imageid;     }  } 

and activity

public class terbarusimascard extends activity {      private progressdialog dialog;     private arraylist<terbarumodel> listterbaru =  null;     private arraylist<childterbaru> listchildterbaru =  null;     private arraylist<arraylist<childterbaru>> listchildxxxxxxxxx = null;     listview list;     string phone1, begdate1, enddate1, img_id1;      @override     protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);         setcontentview(r.layout.sample);         listterbaru = new arraylist<terbarumodel>();         listchildterbaru = new arraylist<childterbaru>();//arraylist<arraylist<childterbaru>>>();         listchildxxxxxxxxx = new arraylist<arraylist<childterbaru>>();         new terbaruasynctask().execute();     }      public class terbaruasynctask extends asynctask<void, void, string> {         string url = ("http:www.domain.com/123");          public terbaruasynctask() {             this.url = url;         }          protected void onpreexecute() {             super.onpreexecute();             dialog = progressdialog.show(terbarusimascard.this, "",                     "melakukan pengambilan data...");         }          @override         protected string doinbackground(void... params) {             string result = "";             try {                 result = connection.get(url);             } catch (exception e) {                 result = "";                 log.d("test", e.getmessage());             }             return result;         }          @override         protected void onpostexecute(string result) {             super.onpostexecute(result);             fetchresponse(result.replace("\n", "").trim());             dialog.dismiss();         }      }      private void fetchresponse(string result) {         if (!result.equals("")) {             try {                 jsonarray jsonarray = new jsonarray(result);                 terbarumodel lt = null;                 childterbaru ct = null;                 (int = 0; < jsonarray.length(); i++) {                     listchildterbaru = new arraylist<childterbaru>();                     jsonobject jsonobject = jsonarray.getjsonobject(i);                      lt = new terbarumodel(jsonobject.optstring("img_id"),                             jsonobject.optstring("nama"),                             jsonobject.optstring("alamat"));                     listterbaru.add(lt);                      //for (int j = 0; j < jsonarray.length(); j++) {                         ct = new childterbaru(jsonobject.optstring("begdate"),                                 jsonobject.optstring("enddate"));                         listchildterbaru.add(ct);//get(i).get(i).add(ct);                      //}                         listchildxxxxxxxxx.add(listchildterbaru);                     expandablelistview list = (expandablelistview) findviewbyid(r.id.expandablelistview);                      terbaruadapter adapter = new terbaruadapter(this,listterbaru, listchildxxxxxxxxx);                     list.setadapter(adapter);                  }              } catch (jsonexception e) {                 e.printstacktrace();             }         }     } } 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -