Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox 'Checked' in ListView is restored after scrolling

I'm having the following problem:

I have a ListActivity, its ListView is composed by an icon, text and a checkbox (using LayoutInflater).

The Adapter for the ListView extends ResourceCursorAdapter (i.e. the data source of the ListView is retrieved from a database, also the Checked status of each row)

Everything works pretty Ok, except when I uncheck/check the checkbox in any row, if I scroll down until the modified checkbox is no longer visible, and then scroll up, the checkbox is restored to its original state.

The database IS modified, this is not the problem (i.e. if I modify a row and exit the activity, and enter again, the modified row is displayed Ok).

My guess is that this has something to do on how the list is rendered because for some reason the ListView "renders" the original state of all rows when it was first populated when scrolling.

I've been looking around for this bug but I don't find anybody that had this problem. I appreciate any advice you have.

like image 954
adrianrdzv Avatar asked Oct 14 '22 07:10

adrianrdzv


1 Answers

This link provides an insight into your problem

EditText items in a scrolling list lose their changes when scrolled off the screen

List rows get recycled. Your Cursor may have 1,000 records, but there are not going to be 1,000 EditText widgets created if you scroll through the list. Rather, there will be 10 or so, depending on how many rows are simultaneously visible. Rows get recycled, and the binding operation will replace the old EditText value with a new value from the Cursor for whatever row just scrolled onto the screen, replacing whatever was there before (previous value from the database or a user-edited value).

like image 176
Muniu Avatar answered Oct 27 '22 07:10

Muniu