Wrong reference in ListView Android? -
hi everyone,
i have issue stuck me 2 day, see image, start process #1 , update process process bar when scroll down see "process bar" run, has same process status first 1 (#1). guess mylistview items reused , use process bar too, 1 meet problem? attach code below,
thank reading
class gridadapter extends baseadapter{ @override public int getcount() { // todo auto-generated method stub return list.size(); } @override public object getitem(int position) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub string currenttext = list.get(position); view cell = convertview; if(cell==null){ layoutinflater inflater = mainactivity.this.getlayoutinflater(); cell = inflater.inflate(r.layout.gird_tiem, null); viewholder viewholder = new viewholder(); viewholder.text = (textview) cell.findviewbyid(r.id.tv_test); viewholder.button = (button) cell.findviewbyid(r.id.button); viewholder.progressbar = (progressbar) cell.findviewbyid(r.id.process); cell.settag(viewholder); } final viewholder holder = (viewholder) cell.gettag(); holder.text.settext(currenttext); holder.button.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub processmanager.onadditemclick(100, holder.progressbar); } }); return cell; } } static class viewholder { public textview text; public button button; public progressbar progressbar; } //my process manager
public class processmanager { activity activity; public processmanager(activity activity) { super(); this.activity = activity; } int temp; public void onadditemclick(final int tasks, final progressbar cell) { new thread(new runnable() { @override public void run() { for(int = 0;i<tasks;i++){ temp = i; thread thread = new thread(new runnable() { @override public void run() { // todo auto-generated method stub } }); thread.run(); try { thread.sleep(500); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } activity.runonuithread(new runnable() { @override public void run() { // todo auto-generated method stub cell.setprogress((temp*100)/tasks); } }); } } }).start(); } //my item is
<?xml version="1.0" encoding="utf-8"?> <textview android:id="@+id/tv_test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="xxxx" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start process" android:id="@+id/button" android:focusable="false" /> <progressbar android:id="@+id/process" style="?android:attr/progressbarstylehorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" /> //my grid view
you need put
final viewholder holder = (viewholder) cell.gettag(); in else bracket after if(cell==null) statement. otherwise it's going create children tag every time (recycling views).
Comments
Post a Comment