user2443607
user2443607

Reputation:

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

Upvotes: 0

Views: 2822

Answers (2)

Timuçin
Timuçin

Reputation: 4673

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

Upvotes: 1

user2443607
user2443607

Reputation:

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!

Upvotes: 0

Related Questions