android - new listview item duplicates action of first listview item when listview scrolls down? -
i have button , progress bar(invisible) in listview. when button clicks should disappear , progress bar should visible , start running (downloading web server) , when done running button should appear again. when click first item's button, progress bar runs if scroll down until first item goes off screen see progress bar running simultaneously first item's progress bar on last item of listview. if scroll first item's progressbar runs normally. happens same if click second item the second last item of listview same. problem , how solve it? please help!!!
here custom listview adapter async task
public static class viewholder { public imageview imgviewflag; public textview txtviewtitle,txtprogress; public button imgar,imgpdf,imgdown,btndel; public textprogressbar progress; public connectiondetector cd=new connectiondetector(activity); public database db=new database(activity); int position; } @override public view getview(final int position, view convertview, viewgroup parent) { final arraylist<string> array=journalarray.get(position); final viewholder view; layoutinflater inflator = activity.getlayoutinflater(); if(convertview==null) { view = new viewholder(); convertview = inflator.inflate(r.layout.familylist_item, null); view.progress=(textprogressbar)convertview.findviewbyid(r.id.downprogress); view.progress.settextcolor(color.black); view.txtviewtitle = (textview) convertview.findviewbyid(r.id.text); //view.txtprogress=(textview)convertview.findviewbyid(r.id.textprogress); view.imgviewflag = (imageview) convertview.findviewbyid(r.id.imageview1); view.imgar=(button)convertview.findviewbyid(r.id.imagear); view.imgar.settag(view); view.imgdown=(button)convertview.findviewbyid(r.id.imagedown); view.imgdown.settag(view); view.imgpdf=(button)convertview.findviewbyid(r.id.imagepdf); view.imgpdf.settag(view); //view.progress=(progressbar)convertview.findviewbyid(r.id.downprogress); view.btndel=(button)convertview.findviewbyid(r.id.btndel); view.btndel.settag(view); convertview.settag(view); } else { view = (viewholder) convertview.gettag(); } if(!isloading){ if(deletebin){ if(xmlparser.checkfileinsd(array.get(5))){ view.btndel.setvisibility(view.visible); view.imgar.setvisibility(view.gone); view.imgdown.setvisibility(view.gone); view.imgpdf.setvisibility(view.gone); view.progress.setvisibility(view.gone); } else{ view.imgdown.setvisibility(view.gone); view.imgar.setvisibility(view.gone); view.btndel.setvisibility(view.gone); view.imgpdf.setvisibility(view.gone); view.progress.setvisibility(view.gone); } } else{ view.btndel.setvisibility(view.gone); if(!xmlparser.checkfileinsd(array.get(5))){ view.imgar.setvisibility(view.gone); view.imgpdf.setvisibility(view.gone); view.imgdown.setvisibility(view.visible); view.progress.setvisibility(view.gone); } else{ view.imgar.setvisibility(view.visible); view.imgpdf.setvisibility(view.visible); view.imgdown.setvisibility(view.gone); view.progress.setvisibility(view.gone); } } } view.txtviewtitle.settext(array.get(1)); channelid=array.get(4); relativelayout.layoutparams paramrel = new relativelayout.layoutparams(width/5, height/5); paramrel.leftmargin = width/10; view.imgviewflag.setlayoutparams(paramrel); view.imgviewflag.setimagedrawable(drawable.createfrompath(new file(array.get(3)).getabsolutepath())); view.imgdown.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(downloading<1){ downloading++; path=array.get(5); channelid=array.get(4); pos=position; view.progress.setmax(100); view.progress.setprogress(0); view.progress.setvisibility(view.visible); view.imgdown.setvisibility(view.gone); view.progress.settext("Татаж байна..."); new asynctask<viewholder, string, string>() { private viewholder v; @override protected string doinbackground(viewholder... params) { v = params[0]; int count; isloading=true; //mprogress=progress; string result="0"; if(view.cd.isconnectingtointernet()){ getchannel(channelid); try { url url = new url(path); urlconnection conection = url.openconnection(); conection.connect(); // getting file length lenghtoffile = conection.getcontentlength(); // input stream read file - 8k buffer inputstream input = new bufferedinputstream(url.openstream(), 8192); string path="/mnt/sdcard/sdcard/.familymagazine"; file dir=new file(path); boolean test=dir.mkdirs(); outputstream output = new fileoutputstream("/mnt/sdcard/sdcard/.familymagazine/"+xmlparser.getfilename(path)); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; // publishing progress.... // after onprogressupdate called publishprogress(""+(int)((total*100)/lenghtoffile)); // writing data file output.write(data, 0, count); } // flushing output output.flush(); // closing streams output.close(); input.close(); } catch (exception e) { result=e.tostring(); } } else result="Интернетэд холбогдоогүй байна!"; return result; } protected void onprogressupdate(string... progress) { // setting progress percentage view.progress.setprogress(integer.parseint(progress[0])); int x=lenghtoffile/100 * integer.parseint(progress[0])/1048576; view.progress.settext(x+"mb / "+lenghtoffile/1048576+"mb"); } @override protected void onpostexecute(string result) { super.onpostexecute(result); //view.txtprogress.setvisibility(view.gone); downloading--; // if (v.position == pos) { isloading=false; if(result.equals("0")){ view.imgdown.setvisibility(view.gone); view.progress.setvisibility(view.gone); view.btndel.setvisibility(view.gone); view.imgar.setvisibility(view.visible); view.imgpdf.setvisibility(view.visible); } else { toast.maketext(activity, result, toast.length_long).show(); view.imgdown.setvisibility(view.visible); view.btndel.setvisibility(view.gone); view.imgar.setvisibility(view.gone); view.imgpdf.setvisibility(view.gone); } } //} }.execute(view); } } }); return convertview;
}
because you're using view holder, views being re-used scroll down. when using view holder, cannot assume view starts in specific state (gone, visible, etc.) if changing in rows. should explicitly set state of views every time.
Comments
Post a Comment