android - ListView displaying same data -


i have large json array. reading data json array , storing in list view, list view repeats same data.

i.e last data json instead of displaying data.

here code.

public class historyactivity extends appcompatactivity { private toolbar toolbar; string strserverresponse = null; progressdialog ndialog; pojo pojo; arraylist<pojo> history; historyadapter myadapter; listview list; public string date, intime, outtime, inlat, inlong;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_history);     toolbar = (toolbar) findviewbyid(r.id.app_bar);     toolbar.settitle("history");     setsupportactionbar(toolbar);     getsupportactionbar().setdisplayhomeasupenabled(true);     list = (listview) findviewbyid(r.id.historylist);     history = new arraylist<pojo>();     new netcheck().execute();  }  private class netcheck extends asynctask<void, void, void> {       @override     protected void onpostexecute(void result) {         // todo auto-generated method stub         super.onpostexecute(result);          ndialog.dismiss();         // todo auto-generated method stub         myadapter = new historyadapter(historyactivity.this, history);         list.setadapter(myadapter);     }      @override     protected void doinbackground(void... params) {         // todo auto-generated method stub          try {              httpclient httpclient = new defaulthttpclient();              httppost httprequest = new httppost(                      "http://myurl");             httprequest.setheader("content-type", "application/json");             sharedpreferences mmm = getsharedpreferences(                     "mypref", mode_private);              string logempid = mmm.getstring("id", null);              jsonobject json = new jsonobject();              json.put("empid", logempid);              log.e("json object", json.tostring());              stringentity se = new stringentity(json.tostring());              se.setcontentencoding("utf-8");             se.setcontenttype("application/json");              httprequest.setentity(se);             httpresponse httpres = httpclient.execute(httprequest);              java.io.inputstream inputstream = httpres.getentity()                     .getcontent();             inputstreamreader inputstreamreader = new inputstreamreader(                     inputstream);              bufferedreader reader = new bufferedreader(inputstreamreader);             stringbuilder sb = new stringbuilder();             string line = null;             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             inputstream.close();             strserverresponse = sb.tostring();             log.e("server response", "" + strserverresponse.tostring());              if (strserverresponse != null) {                 try {                      jsonarray arr = new jsonarray(strserverresponse);                      (int k = 0; k < arr.length(); k++) {                          jsonobject jsonobj1 = arr.getjsonobject(k);                          pojo = new pojo();                         date = jsonobj1.optstring("login_date");                         intime = jsonobj1.optstring("login_time");                         outtime = jsonobj1.optstring("logout_time");                          log.e("login time server",""+intime);                         log.e("login out server",""+outtime);                         log.e("login date server",""+date);                         pojo.setdate(date);                         pojo.setlogintime(intime);                         pojo.setlogouttime(outtime);                          jsonarray subarraylat = jsonobj1.getjsonarray("lati_long");                         (int = 0; < subarraylat.length(); i++) {                             string lat = subarraylat.getjsonobject(i).getstring("latitude").tostring();                             string loong = subarraylat.getjsonobject(i).getstring("longitude").tostring();                             log.e("jsonarray lat", "" + lat);                             log.e("jsonarray longg", "" + loong);                             pojo.setlat(lat);                             pojo.setlong(loong);                         }                          history.add(pojo);                     }                  } catch (jsonexception e) {                     e.printstacktrace();                }       } 

here adapter.

 public class historyadapter extends baseadapter { private context activity; textview tv_date; textview tv_logintime; textview tv_logouttime; textview tv_details; pojo pojo; arraylist<pojo> list; private arraylist<pojo> arraylist = null; public static layoutinflater inflater; private context context;  public historyadapter(context context) {     this.context = context; }   public historyadapter(context a, arraylist<pojo> history) {     // todo auto-generated constructor stub     activity = a;     list = history;     inflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service);     this.arraylist = new arraylist<pojo>();     this.arraylist.addall(list);  }  @override public int getcount() {     // todo auto-generated method stub     return list.size(); }  @override public object getitem(int position) {     // todo auto-generated method stub     return list.get(position); }  @override public long getitemid(int position) {     // todo auto-generated method stub     return position; }  @override public view getview(int position, view convertview, viewgroup parent) {     // todo auto-generated method stub     view v = convertview;       v = inflater.inflate(r.layout.history_item, parent, false);     pojo = list.get(position);     tv_date = (textview) v.findviewbyid(r.id.historydate);     tv_logintime = (textview) v.findviewbyid(r.id.historylogintime);     tv_logouttime = (textview) v.findviewbyid(r.id.historylogouttime);     tv_details = (textview) v.findviewbyid(r.id.historydetails);      tv_date.settext(pojo.getdate());     tv_logintime.settext(pojo.getlogintime());     tv_logouttime.settext(pojo.getlogouttime());     final string lat = pojo.getlat().tostring();     string longg = pojo.getlong().tostring();     log.e("adapter latitude", "" + lat);     log.e("adapter longitude", "" + longg);     tv_details.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent = new intent(context, mapactivity.class);             i.putextra("lat", "" + lat);             i.putextra("longg", "+longg");             context.startactivity(i);         }     });      return v;  }   } 

this json data

[   {     "login_time": "10:30:28",     "logout_time": "10:31:47",     "login_date": "2015- 09-30",     "lati_long": [       {         "date_time": "2015:09:30  11:14:53",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:01",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:15",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:14:53",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:01",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:15",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:52",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:17:16",         "latitude": "21.121776",         "longitude": "79.047563"       }     ]   },   {     "login_time": "10:42:56",     "logout_time": "10:44:41",     "login_date": "2015-09-30",     "lati_long": [       {         "date_time": "2015:09:30 11:14:53",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:01",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:15",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:14:53",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:01",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:15",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:52",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:17:16",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:14:53",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:01",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:15",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:14:53",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:01",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:15",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:15:52",         "latitude": "21.121776",         "longitude": "79.047563"       },       {         "date_time": "2015:09:30 11:17:16",         "latitude": "21.121776",         "longitude": "79.047563"       }     ]   } ] 

is there wrong here? please me.

you have declared customobject pojo's variable global variable. declare local variable

for (int k = 0; k < arr.length(); k++) {           jsonobject jsonobj1 = arr.getjsonobject(k);          pojo pojo = new pojo();          date = jsonobj1.optstring("login_date");          intime = jsonobj1.optstring("login_time");          outtime = jsonobj1.optstring("logout_time");           log.e("login time server",""+intime);          log.e("login out server",""+outtime);          log.e("login date server",""+date);          pojo.setdate(date);          pojo.setlogintime(intime);          pojo.setlogouttime(outtime);           jsonarray subarraylat = jsonobj1.getjsonarray("lati_long");          (int = 0; < subarraylat.length(); i++) {                  string lat = subarraylat.getjsonobject(i).getstring("latitude").tostring();                  string loong = subarraylat.getjsonobject(i).getstring("longitude").tostring();                  log.e("jsonarray lat", "" + lat);                  log.e("jsonarray longg", "" + loong);                  pojo.setlat(lat);                  pojo.setlong(loong);           }           history.add(pojo); } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -