How do I stop Android ListView from resetting values after scrolling? -
i have listview custom adapter, , each row contains seekbar user can set. each seekbar has seekbarchangelistener set in adapter, , when user changes value of seekbar in listview, adapter's arraylist updated. i've got in getview()
of adapter:
@override public view getview(final int position, view convertview, final viewgroup parent) { final viewholder viewholder; view v = convertview; if(v == null){ v = inflater.inflate(r.layout.row, null); viewholder = new viewholder(); viewholder.contentview = (textview) v.findviewbyid(r.id.pref_content); viewholder.sliderview = (seekbar) v.findviewbyid(r.id.pref_slider); v.settag(viewholder); }else{ viewholder = (viewholder) v.gettag(); } // initial values prompt p = promptlist.get(position); string content = p.getcontent(); int sliderlevel = p.sliderlevel(); // set views initial values viewholder.contentview.settext(content); viewholder.sliderview.setmax(nooflevels); viewholder.sliderview.setprogress(sliderlevel); // set change listener seekbar viewholder.sliderview.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { // set new level of slider in array list promptlist.get(position).setsliderlevel(progress); } }); return v; }
so when user changes value of seekbar changelistener gets object arraylist , sets new sliderlevel value. however, if set new value in listview , scroll down again, getview called again, seekbar returns initial value, though have changed value in arraylist.
can show me i'm going wrong here? thanks
the method onprogresschanged()
called every time user changes seekbar
value. called "by system" (i think happens because seekbar
recycled part of convertview). that's why necessary check whether fromuser
true:
@override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { if (!fromuser) return; // set new level of slider in array list promptlist.get(position).setsliderlevel(progress); }
in case, not have call notifydatasetchanged()
because long 1 specific seekbar
still shown, user change progress. once hidden (because user scrolls) , data position have shown again later, getview()
called , correct value set seekbar
in convertview happens display data then.
one other thought (performance reasons): consider changing progress value of promptlist.get(position)
user stops manipulating slider. this, can override onstoptrackingtouch()
instead of onprogresschanged()
, write
promptlist.get(position).setsliderlevel(seekbar.getprogress());
Comments
Post a Comment