简体   繁体   English

ListView中CheckBox的检查状态

[英]CheckBox checked state in a ListView

I'm using a ListView with CheckBox, but as most of you know, when you roll down the scroll, a checked CheckBox gets unchecked as you roll up back the scroll. 我正在将ListView与CheckBox一起使用,但是众所周知,当您向下滚动时,选中的CheckBox在向上滚动时会被取消选中。 So i've been reading and i found out that you can pass (using getView) the id of the CB to the position parameter of getView to save the CheckBox state! 因此,我一直在阅读,发现可以将CB的ID传递(使用getView)到getView的position参数以保存CheckBox状态!

But i can't use getView with SimpleCursorAdapter, can i? 但是我不能将getView与SimpleCursorAdapter一起使用,可以吗? Because i'm using bindView! 因为我正在使用bindView!

Thanks 谢谢

What is happening is recycling. 发生的事情是回收。 7 rows fit in your screen and when you scroll down, the top one is being recycled for the new one at the bottom. 屏幕上可容纳7行,向下滚动时,顶部的将被回收,底部的将被回收。 What you should do is to save the states of the checkboxes. 您应该做的是保存复选框的状态。

Here is a good solution to a similar problem: 这是解决类似问题的好方法:

https://github.com/commonsguy/cw-android/tree/master/FancyLists/RateList https://github.com/commonsguy/cw-android/tree/master/FancyLists/RateList

I managed to get the checkbox state restored after i scroll up/down using setViewBinder (saw it in another answer): 使用setViewBinder向上/向下滚动后,我设法恢复了复选框状态(在另一个答案中看到了它):

    mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {            
        if(columnIndex==4) {
            cb=(CheckBox)view;
            cb.setChecked(cursor.getInt(4)==0? false:true);
            return true;
        }
    return false;
    }
    });

But still something weird happens, the CheckBox is being recycled after 7 or 8 positions. 但是仍然发生了一些奇怪的事情,CheckBox在7或8个位置后被回收。 If i check the first CheckBox and theres more than 10 positions/rows, the 8th is also checked, same happens when i check the last one, 8 positions up there will be a checked CheckBox. 如果我检查第一个CheckBox并且有10个以上的位置/行,则还会检查第8个,当我检查最后一个时,也会发生同样的情况,上面8个位置将有一个Checked CheckBox。

Any thoughs? 可以吗 Ideias? 爱迪亚? Help! 救命!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM