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

五迷三道 提交于 2019-11-28 14:15:38

But...if I then scroll the list far enough to take the edited item off the screen, when I scroll back to look at it again its value has returned to what it was before I made the changes, ie. my edits have been lost.

Of course.

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

And, since a regular Cursor is immutable, you have no way of persisting any edits in a way that will transparently be put back into the list.

I suspect it is possible to create a ListView with rows that are EditTexts, probably by creating a custom Adapter class and handling all the row recycling yourself. However, it is going to be a fair amount of work, and the built-in classes will only give you a bit of support for this pattern.

defpillz

I searched all over SO for an answer to this exact issue and was finally able to resolve the problem with an onFocusChangeListener on my EditText within the adapter. I posted my solution here: https://stackoverflow.com/a/13312282/1812518

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!