android - Whole listview gets duplicated -
i've listview shows values web service. fine when loads listview. when click on item open new fragment (which shows details of item) , same listview, items doubles everytime. example, if have (in original) 5 items in total. when fragment, total count 10, 15 , on. means app adding items again , again. searched solution , did general solution adapter.notifydatasetchanged()
still gets duplicated. if can show me problem, grateful.
public class customlistadapter extends arrayadapter<model> { private context mcontext; int resource; private arraylist<model> mlistdata = new arraylist<model>(); public customlistadapter(context mcontext, int resource, arraylist<model> mlistdata) { super(mcontext, resource, mlistdata); this.resource = resource; this.mcontext = mcontext; this.mlistdata = mlistdata; } public void setlistdata(arraylist<model> mlistdata) { this.mlistdata = mlistdata; notifydatasetchanged(); } @override public int getcount() { return super.getcount(); } @override public view getview(final int position, view convertview, final viewgroup parent) { view v = convertview; final viewholder holder; if (v == null) { holder = new viewholder(); layoutinflater inflater = ((activity) mcontext).getlayoutinflater(); v = inflater.inflate(resource, parent, false); holder.custname = (textview) v.findviewbyid(r.id.cust_name); holder.date = (textview) v.findviewbyid(r.id.date); holder.staffname = (textview) v.findviewbyid(r.id.staff_name); holder.time = (textview) v.findviewbyid(r.id.time); holder.service = (textview) v.findviewbyid(r.id.service); holder.refid = (textview) v.findviewbyid(r.id.refid); v.settag(holder); } else { holder = (viewholder) v.gettag(); } final model item = mlistdata.get(position); holder.custname.settext(item.getcustname()); holder.date.settext(item.getdate()); holder.staffname.settext(item.getstaffname()); holder.time.settext(item.gettime()); holder.service.settext(item.getservice()); holder.refid.settext(item.getrefid()); return v; } class viewholder { textview custname, date, staffname, time, service, refid; } }
public class listfrag extends fragment{ private swipemenulistview listview; private customlistadapter adapter; private cshowprogress cshowprogress; private sqlitehandler db; private string uid, bookingid; private arraylist<model> arraylist = new arraylist<>(); private static final string booked_list = "http://192.168.220.13/android/showbookinglist"; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { return inflater.inflate(r.layout.listfrag, container, false); } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); listview = (swipemenulistview)view.findviewbyid(r.id.list); db = new sqlitehandler(getactivity()); cshowprogress = cshowprogress.getinstance(); fetchdetails(); adapter = new customlistadapter(getactivity(), r.layout.listitem, arraylist); listview.setadapter(adapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { string = arraylist.get(i).getcustname(); string b = arraylist.get(i).getstaffname(); string c = arraylist.get(i).getservice(); string d = arraylist.get(i).getdate(); string e = arraylist.get(i).gettime(); string f = arraylist.get(i).getrefid(); string g = arraylist.get(i).getemail(); string h = arraylist.get(i).getservprice(); string j = arraylist.get(i).getspeclprice(); string k = arraylist.get(i).getstatus(); db.adddetails(a, b, c, d, e, f, g, h, j, k); detailsview details = new detailsview(); ((bookings)getactivity()).replacefragment(details); } }); } private void fetchdetails() { cshowprogress.showprogress(getactivity()); stringrequest stringrequest = new stringrequest(request.method.post, booked_list, new response.listener<string>() { @override public void onresponse(string response) { cshowprogress.hideprogress(); try { jsonarray jsonarray = new jsonarray(response); for(int i=0; i<jsonarray.length(); i++){ jsonobject obj = jsonarray.getjsonobject(i); model model = new model(); model.setcustname(obj.getstring("customername")); model.setdate(obj.getstring("staffdate")); model.setstaffname(obj.getstring("staffname")+"(staff)"); model.settime(obj.getstring("stafftime")); model.setservice(obj.getstring("servicename")); model.setrefid(obj.getstring("booking_referenceid")); model.setemail(obj.getstring("customeremail")); model.setservprice(obj.getstring("serviceprice")); model.setspeclprice(obj.getstring("specialprice")); model.setstatus(obj.getstring("status")); model.setbookid(obj.getstring("bookingid")); arraylist.add(model); } adapter.notifydatasetchanged(); } catch (jsonexception e) { // json error e.printstacktrace(); toast.maketext(getactivity(), "json error: " + e.getmessage(), toast.length_long).show(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { toast.maketext(getactivity(), "volleyerror" + error.tostring(), toast.length_long).show(); } }) { @override protected map<string, string> getparams() { map<string, string> params = new hashmap<string, string>(); params.put("spaid", "145"); return params; } }; requestqueue requestqueue = volley.newrequestqueue(getactivity()); requestqueue.add(stringrequest); } }
try change as.. b'coz adding elements in created list.. have clear it.
private void fetchdetails() { if(arraylist!=null )arraylist.clear(); // line cshowprogress.showprogress(getactivity());
Comments
Post a Comment