java - After scrolling in the listview, elements are swapped -
i writing time-tracking app, every row of listview has 2 buttons(start , stop) , textview display elapsed time.
after scrolling in listview, elements swapped.
i'm not sure in solution, put in model class, viewholder object getting acces changing view elements.
here fragment of code model
private string name; private boolean isstart=false; private long elapsedtime=0l,seconds=0l,hours=0l,minutes=0l,lastpause=0l,updatetime=0l,starttime=0l,days=0l; private runnable updatetimethread=new runnable() { @override public void run() { if(isstart && starttime!=0) { updatetime = ((system.currenttimemillis() - starttime) + lastpause); seconds = updatetime / 1000; minutes = seconds / 60; hours = minutes / 60; seconds = seconds % 60; minutes = minutes % 60; hours = hours % 24; holder.days.settext(string.format("%04d", days)); holder.hours.settext(string.format("%02d", hours)); holder.minutes.settext(string.format("%02d", minutes)); holder.seconds.settext(string.format("%02d", seconds)); log.d("mytag",name+" "+seconds); mainactivity.handler.post(this); } } }; myadapter.viewholder holder; public myadapter.viewholder getholder() { return holder; } public void setholder(myadapter.viewholder holder) { this.holder = holder; } public runnable getrunnable() { return updatetimethread; }
adapter's fragment code
public view getview(final int position, final view convertview, viewgroup parent) { view row = convertview; final tracker tracker = trackerlist.get(position); final runnable updatetimethread=tracker.getrunnable(); view.onclicklistener onclicklistener; viewholder holder; if(row == null){ holder = new viewholder(); layoutinflater inflater = ((activity)context).getlayoutinflater(); row = inflater.inflate(r.layout.row,parent,false); holder.name = (textview)row.findviewbyid(r.id.tvname); holder.days = (textview)row.findviewbyid(r.id.tvdays); holder.hours = (textview)row.findviewbyid(r.id.tvhours); holder.minutes = (textview)row.findviewbyid(r.id.tvminutes); holder.seconds = (textview)row.findviewbyid(r.id.tvseconds); holder.start = (button)row.findviewbyid(r.id.btstart); holder.stop = (button)row.findviewbyid(r.id.btstop); row.settag(holder); }else { holder = (viewholder) row.gettag(); } holder.start.setenabled(true); holder.stop.setenabled(false); holder.name.settext(tracker.getname()); final viewholder finalholder = holder; if(tracker.getisstart()){ holder.start.setenabled(false); holder.stop.setenabled(true); } onclicklistener = new view.onclicklistener() { @override public void onclick(view v) { switch (v.getid()) { case r.id.btstart: tracker.setstarttime(system.currenttimemillis()); tracker.setisstart(true); tracker.setholder(finalholder); mainactivity.handler.post(updatetimethread); finalholder.start.setenabled(false); finalholder.stop.setenabled(true); break; case r.id.btstop: tracker.setlastpause(tracker.getupdatetime()); mainactivity.handler.removecallbacks(updatetimethread); finalholder.stop.setenabled(false); finalholder.start.setenabled(true); tracker.setisstart(false); break; } } }; holder.start.setonclicklistener(onclicklistener); holder.stop.setonclicklistener(onclicklistener); return row; } static class viewholder{ textview name,days,hours,minutes,seconds; button start,stop; }
change part
holder.start.setenabled(true); holder.stop.setenabled(false); holder.name.settext(tracker.getname()); final viewholder finalholder = holder; if(tracker.getisstart()){ holder.start.setenabled(false); holder.stop.setenabled(true); }
to this
final viewholder finalholfder = holder; finalholfder.start.setenabled(true); finalholfder.stop.setenabled(false); finalholfder.name.settext(tracker.getname()); if(tracker.getisstart()){ finalholfder.start.setenabled(false); finalholfder.stop.setenabled(true); } else{ finalholfder.start.setenabled(true); finalholfder.stop.setenabled(false); }
and this
holder.start.setonclicklistener(onclicklistener); holder.stop.setonclicklistener(onclicklistener);
to this
finalholfder.start.setonclicklistener(onclicklistener); finalholfder.stop.setonclicklistener(onclicklistener);
Comments
Post a Comment