简体   繁体   中英

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. 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!

But i can't use getView with SimpleCursorAdapter, can i? Because i'm using 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. 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

I managed to get the checkbox state restored after i scroll up/down using setViewBinder (saw it in another answer):

    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. 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.

Any thoughs? Ideias? Help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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