android - ListView shows repeated data everytime the fragment is created -
here code of fragment accesing web service in asynctask fetch data , display in list lazy loading images..when fragment created first time output correct displays 7 items in list should , problem when fragment recreated after being destroyed data repeated i.e if started second time list shows 14 items (7 earlier , 7 again fetched) , happens everytime recreates , no. of items in list 7 more previous.. although in on destroy cleared data of adapter , adapter.getcount() shows 0 still problem exists.
public class servicecarlistfragment extends fragment { private string url; private arraylist<cardetail> cardetaillist = new arraylist<cardetail>(); private carlistadapter adapter; private listview mlist ; private progressdialog progressdialog; @override public void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); log.d("services", "on create"); url = getactivity().getintent().getstringextra("url"); adapter = new carlistadapter(getactivity() , cardetaillist); new downloadcardetail().execute(url); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub log.d("services", "on createview"); view v = inflater.inflate(r.layout.fragment_service_car_list, container,false); mlist = (listview)v.findviewbyid(r.id.list); mlist.setadapter(adapter); return v; } class downloadcardetail extends asynctask<string, string, arraylist<cardetail>>{ @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); progressdialog = progressdialog.show(getactivity(), null, "loading...",true); } @override protected arraylist<cardetail> doinbackground(string... params) { // todo auto-generated method stub arraylist<cardetail> cardetaillist = jsonparser.parsejson(params[0]); return cardetaillist; } @override protected void onpostexecute(arraylist<cardetail> cardetaillist) { // todo auto-generated method stub //servicecarlistfragment.this.cardetaillist = cardetaillist; //adapter = new carlistadapter(getactivity(),servicecarlistfragment.this.cardetaillist); //mlist.setadapter(adapter); progressdialog.dismiss(); servicecarlistfragment.this.cardetaillist.addall(cardetaillist); adapter.notifydatasetchanged(); (cardetail car : cardetaillist) { // start loading images each student car.loadimage(adapter); } } } @override public void ondestroy() { // todo auto-generated method stub super.ondestroy(); cardetaillist.clear(); adapter.notifydatasetchanged(); log.d("services", string.valueof(adapter.getcount())); } @override public void ondestroyview() { // todo auto-generated method stub super.ondestroyview(); log.d("services", "on destroyview"); } @override public void ondetach() { // todo auto-generated method stub super.ondetach(); log.d("services", "on detach"); } @override public void onattach(activity activity) { // todo auto-generated method stub super.onattach(activity); log.d("services", "on attach"); } } this custom adapter using
public class carlistadapter extends baseadapter { private arraylist<cardetail> items = new arraylist<cardetail>(); private context context; public carlistadapter(context context , arraylist<cardetail> items) { super(); this.context = context; this.items = items; } @override public int getcount() { // todo auto-generated method stub return items.size(); } @override public object getitem(int position) { // todo auto-generated method stub return items.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 log.d("inside", "getview"); layoutinflater minflater = (layoutinflater)context.getsystemservice(activity.layout_inflater_service); viewholder holder = null; cardetail car = items.get(position); if (convertview == null) { convertview = minflater.inflate(r.layout.car_list_row, parent , false); holder = new viewholder(); holder.tvcarname = (textview) convertview.findviewbyid(r.id.tvcarname); holder.tvdailypricevalue = (textview) convertview.findviewbyid(r.id.tvweeklypricevalue); holder.tvweeklypricevalue = (textview) convertview.findviewbyid(r.id.tvweeklypricevalue); holder.imgcar = (imageview) convertview.findviewbyid(r.id.imgcar); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.tvcarname.settext(car.getcarname()); if (car.getimage() != null) { holder.imgcar.setimagebitmap(car.getimage()); } else { // default image holder.imgcar.setimageresource(r.drawable.ic_action_call); } return convertview; } static class viewholder { textview tvcarname; textview tvdailypricevalue; textview tvweeklypricevalue; imageview imgcar; } } this model class
public class cardetail { private string carid; private string carname; private string imageurl; private string thumburl; private string dailyprice; private string weeklyprice; private string weekendprice; private string deposit; private string minimumage; private string color; private string make; private string location; private string bodytype; private string fueltype; private string transmission; private string cartype; private string model; private string description; private bitmap image; private bitmap thumbimage; private carlistadapter caradapter; public cardetail() { super(); // todo auto-generated constructor stub } public cardetail(string carid, string carname, string imageurl, string thumburl, string dailyprice, string weeklyprice, string weekendprice, string deposit, string minimumage, string color, string make, string location, string bodytype, string fueltype, string transmission, string cartype, string model, string description) { super(); this.carid = carid; this.carname = carname; this.imageurl = imageurl; this.thumburl = thumburl; this.dailyprice = dailyprice; this.weeklyprice = weeklyprice; this.weekendprice = weekendprice; this.deposit = deposit; this.minimumage = minimumage; this.color = color; this.make = make; this.location = location; this.bodytype = bodytype; this.fueltype = fueltype; this.transmission = transmission; this.cartype = cartype; this.model = model; this.description = description; // loaded later - or can set default image this.image = null; this.thumbimage = null; } public string getcarid() { return carid; } public void setcarid(string carid) { this.carid = carid; } public string getcarname() { return carname; } public void setcarname(string carname) { this.carname = carname; } public string getimageurl() { return imageurl; } public void setimageurl(string imageurl) { this.imageurl = imageurl; } public string getthumburl() { return thumburl; } public void setthumburl(string thumburl) { this.thumburl = thumburl; } public string getdailyprice() { return dailyprice; } public void setdailyprice(string dailyprice) { this.dailyprice = dailyprice; } public string getweeklyprice() { return weeklyprice; } public void setweeklyprice(string weeklyprice) { this.weeklyprice = weeklyprice; } public string getweekendprice() { return weekendprice; } public void setweekendprice(string weekendprice) { this.weekendprice = weekendprice; } public string getdeposit() { return deposit; } public void setdeposit(string deposit) { this.deposit = deposit; } public string getminimumage() { return minimumage; } public void setminimumage(string minimumage) { this.minimumage = minimumage; } public string getcolor() { return color; } public void setcolor(string color) { this.color = color; } public string getmake() { return make; } public void setmake(string make) { this.make = make; } public string getlocation() { return location; } public void setlocation(string location) { this.location = location; } public string getbodytype() { return bodytype; } public void setbodytype(string bodytype) { this.bodytype = bodytype; } public string getfueltype() { return fueltype; } public void setfueltype(string fueltype) { this.fueltype = fueltype; } public string gettransmission() { return transmission; } public void settransmission(string transmission) { this.transmission = transmission; } public string getcartype() { return cartype; } public void setcartype(string cartype) { this.cartype = cartype; } public string getmodel() { return model; } public void setmodel(string model) { this.model = model; } public string getdescription() { return description; } public void setdescription(string description) { this.description = description; } public bitmap getimage() { return image; } public void setimage(bitmap image) { this.image = image; } public bitmap getthumbimage() { return thumbimage; } public void setthumbimage(bitmap thumbimage) { this.thumbimage = thumbimage; } public void loadimage(carlistadapter caradapter) { // hold reference adapter this.caradapter = caradapter; if (thumburl != null && !thumburl.equals("")) { new imageloadtask().execute(thumburl); } } // async task avoid choking ui thread private class imageloadtask extends asynctask<string, string, bitmap> { @override protected void onpreexecute() { log.i("imageloadtask", "loading image..."); } // param[0] img url protected bitmap doinbackground(string... param) { log.i("imageloadtask", "attempting load image url: " + param[0]); try { bitmap b = jsonparser.downloadbitmap(param[0]); return b; } catch (exception e) { e.printstacktrace(); return null; } } protected void onprogressupdate(string... progress) { // no op } protected void onpostexecute(bitmap ret) { if (ret != null) { log.i("imageloadtask", "successfully loaded " + carname + " image"); image = ret; if (caradapter != null) { // when image loaded notify adapter caradapter.notifydatasetchanged(); } } else { log.e("imageloadtask", "failed load " + carname + " image"); } } }
you need clear arraylist data before next call of async method. in case cardetaillis.clear() new downloadcardetail().execute(url); or check flow , clear it.
Comments
Post a Comment