Android: ListView from MySQL only display the last element -
i'm trying retrieve data mysql database , put on listview, works fine, put data textviews(dynamically) , works fine. when used listview, last element displayed, think means every new element overwritten old one, right?
what can solve this? here's code tell what's wrong??
public class makeappointementactivity extends appcompatactivity { public list<availabilitylist> customlist; public listview lv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_make_appointement); lv=(listview)findviewbyid(r.id.listview); intent intent=getintent(); new retrievetask().execute(); } private class retrievetask extends asynctask<void, void, string> { @override protected string doinbackground(void... params) { string strurl = "availableappointments1.php"; url url; stringbuffer sb = new stringbuffer(); try { url = new url(strurl); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.connect(); inputstream istream = connection.getinputstream(); bufferedreader reader = new bufferedreader(new inputstreamreader(istream)); string line; while( (line = reader.readline()) != null){ sb.append(line); } reader.close(); istream.close(); } catch (ioexception e) { e.printstacktrace(); } return sb.tostring(); } @override protected void onpostexecute(string result) { super.onpostexecute(result); new parsertask().execute(result); } } // background thread parse json data retrieved mysql server private class parsertask extends asynctask<string, void, list<hashmap<string, string>>> { @override protected list<hashmap<string, string>> doinbackground(string... params) { appointementjsonparser appointementparser = new appointementjsonparser(); jsonobject json = null; try { json = new jsonobject(params[0]); } catch (jsonexception e) { e.printstacktrace(); } return appointementparser.parse(json); } @override protected void onpostexecute(list<hashmap<string, string>> result) { customlist=new arraylist<>(); / move here (int = 0; < result.size(); i++) { hashmap<string, string> appointement = result.get(i); string fromt = appointement.get("fromt"); string tot = appointement.get("tot"); string date = appointement.get("date"); addavailableappoint(fromt,tot,date); } updatelistview(); // update listview when add data arraylist } } private void addavailableappoint(final string fromt, final string tot, final string date) { customlist.add(new availabilitylist(fromt)); } // split new function update listview private updatelistview(){ arrayadapter adapter=new doctoravailabilityadapter(makeappointementactivity.this,r.layout.list_items,customlist); adapter.notifydatasetchanged(); lv.setadapter(adapter); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(makeappointementactivity.this, appointementactivity.class); intent.putextra("fromt", fromt); intent.putextra("tot", tot); intent.putextra("date", date); startactivity(intent); } }); } }
try code
.....// above code @override protected void onpostexecute(list<hashmap<string, string>> result) { customlist=new arraylist<>(); / move here (int = 0; < result.size(); i++) { hashmap<string, string> appointement = result.get(i); string fromt = appointement.get("fromt"); string tot = appointement.get("tot"); string date = appointement.get("date"); addavailableappoint(fromt,tot,date); } updatelistview(); // update listview when add data arraylist } } private void addavailableappoint(final string fromt, final string tot, final string date) { customlist.add(new availabilitylist(fromt)); } // split new function update listview private updatelistview(){ arrayadapter adapter=new doctoravailabilityadapter(makeappointementactivity.this,r.layout.list_items,customlist); adapter.notifydatasetchanged(); lv.setadapter(adapter); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(makeappointementactivity.this, appointementactivity.class); // intent.putextra("fromt", fromt); // change intent.putextra("fromt", customlist.get(position).getfromt()); intent.putextra("tot", tot); intent.putextra("date", date); startactivity(intent); } }); } } hope help
Comments
Post a Comment