android - Adapter not dislpaying correct data -
i new android. develping application in getting large json data server. want display in list view. getting same value repeated. no of items shown list view proper. same data repeated in list items.
here code.
public class historyactivity extends appcompatactivity { private toolbar toolbar; string strserverresponse = null; progressdialog ndialog; arraylist<string>clicklat; arraylist<string>clicklong; arraylist<string>dttime; 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"); clicklat=new arraylist<string>(); clicklong=new arraylist<string>(); dttime=new arraylist<string>(); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); list = (listview) findviewbyid(r.id.historylist); history = new arraylist<pojo>(); new netcheck().execute(); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { arraylist<string>clicklat= new arraylist<string>(history.get(position).getlati()); arraylist<string>clicklong= new arraylist<string>(history.get(position).getlongi()); arraylist<string>dttime= new arraylist<string>(history.get(position).getdatetime()); intent = new intent(historyactivity.this, detailsactivity.class); i.putstringarraylistextra("clicklat", clicklat); i.putstringarraylistextra("clicklong", clicklong); i.putstringarraylistextra("clickdatetime", dttime); startactivity(i); } }); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: finish(); return true; default: return super.onoptionsitemselected(item); } } 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 pojo = new pojo(); jsonarray subarraylat = jsonobj1.getjsonarray("lati_long"); list<string> lati= new arraylist<string>(); list<string> longi= new arraylist<string>(); list<string> dateandtime= new arraylist<string>(); (int = 0; < subarraylat.length(); i++) { string lat = subarraylat.getjsonobject(i).getstring("latitude").tostring(); string loong = subarraylat.getjsonobject(i).getstring("longitude").tostring(); string datetimee = subarraylat.getjsonobject(i).getstring("date_time").tostring(); lati.add(lat); longi.add(loong); dateandtime.add(datetimee); } pojo.setlati(lati);//adding latitude list pojo.setlongi(longi); //adding longitude list pojo.setdatetime(dateandtime); string dateee = arr.getjsonobject(k).getstring("login_date"); string timeeee = arr.getjsonobject(k).getstring("login_time"); string timeeee2 = arr.getjsonobject(k).getstring("logout_time"); pojo.setdate(dateee); pojo.setlogintime(timeeee); pojo.setlogouttime(timeeee2); history.add(pojo); } } catch (jsonexception e) { e.printstacktrace(); }
and adapter
public class historyadapter extends baseadapter { private context activity; textview tv_date; textview tv_logintime; textview tv_logouttime; arraylist<pojo> list; private arraylist<pojo> arraylist = null; public static layoutinflater inflater; private 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; if(convertview == null) { v = inflater.inflate(r.layout.history_item, parent, false); } final pojo 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_date.settext(pojo.getdate()); tv_logintime.settext(pojo.getlogintime()); tv_logouttime.settext(pojo.getlogouttime()); return v; } }
and setters , getters
public class pojo { public static string empid11; public static string logintime; public static string date; public static string logouttime; public static list<string> lat; public static list<string> datetime; public static list<string> longi; public static list<string> inlogin; public static list<string> indate; public list<string> getintime(){ return this.inlogin; } public list<string> getindate(){ return this.indate; } public void setindate(list<string> indate){ this.indate = indate; } public list<string> getlati(){ return this.lat; } public list<string> getlongi(){ return this.longi; } public void setlati(list<string> lat){ this.lat = lat; } public void setlongi(list<string> longi){ this.longi = longi; } public void setid(string empid) { this.empid11 = empid; } public string getid() { return empid11; } public void setlogintime(string logintime) { this.logintime = logintime; } public string getlogintime() { return logintime; } public void setlogouttime(string logouttime) { this.logouttime = logouttime; } public string getlogouttime() { return logouttime; } public void setdate(string date) { this.date = date; } public string getdate() { return date; } public list<string> getdatetime(){ return this.datetime; } public void setdatetime(list<string> datetime){ this.datetime = datetime; } }
please me. have tried lot. dont undetstand wrong here.
hello see modifiy version of adapter. let me know if have problem it.
public class historyadapter extends baseadapter { private context activity; textview tv_date; textview tv_logintime; textview tv_logouttime; private arraylist<pojo> arraylist = null; public static layoutinflater inflater; private context context; public historyadapter(context a) { activity = a; inflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service); this.arraylist = new arraylist<pojo>(); } @override public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return position; } public void addhistorydata(arraylist<pojo> newdataset){ if(arraylist != null){ arraylist.addall(newdataset); } } @override public view getview(int position, view convertview, viewgroup parent) { viewholder mviewholder = null; if(convertview == null) { mviewholder = new viewholder(); convertview = inflater.inflate(r.layout.history_item, parent, false); mviewholder.tv_date = (textview) convertview.findviewbyid(r.id.historydate); mviewholder.tv_logintime = (textview)convertview.findviewbyid(r.id.historylogintime); mviewholder.tv_logouttime = (textview)convertview.findviewbyid(r.id.historylogouttime); convertview.settag(mviewholder); }else{ mviewholder = (viewholder) convertview.gettag(); } final pojo pojo = list.get(position); mviewholder.tv_date.settext(pojo.getdate()); mviewholder.tv_logintime.settext(pojo.getlogintime()); mviewholder.tv_logouttime.settext(pojo.getlogouttime()); return v; } public class viewholder{ textview tv_date textview tv_logintime; textview tv_logouttime; } }
in next activity netcheck
class when web service response come change below :
history.add(pojo); myadapter.addhistorydata(history); myadapter.notifiydatasetchanged();
Comments
Post a Comment