滚动列表中的 EditText 项目在滚动离开屏幕时会丢失其更改 [英] EditText items in a scrolling list lose their changes when scrolled off the screen

查看:20
本文介绍了滚动列表中的 EditText 项目在滚动离开屏幕时会丢失其更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 SimpleCursorAdapter 创建并预先填充了 SQLite 数据库中的值的 EditText 项的长滚动列表.

I have a long scrolling list of EditText items created by a SimpleCursorAdapter and prepopulated with values from an SQLite database.

我是这样做的:

cursor = db.rawQuery("SELECT _id, criterion, localweight, globalweight FROM " + dbTableName + " ORDER BY criterion", null);

startManagingCursor(cursor);

mAdapter = new SimpleCursorAdapter(this, R.layout.weight_edit_items, cursor, new String[]{"criterion","localweight","globalweight"}, new int[]{R.id.criterion_edit, R.id.localweight_edit, R.id.globalweight_edit});    

this.setListAdapter(mAdapter);

滚动列表有几个模拟器屏幕长.项目显示 OK - 滚动浏览它们会显示每个项目都具有来自数据库的正确值.

The scrolling list is several emulator screens long. The items display OK - scrolling through them shows that each has the correct value from the database.

我可以对任何 EditText 进行编辑更改,新文本被接受并显示在框中.

I can make an edit change to any of the EditTexts and the new text is accepted and displayed in the box.

但是...如果我将列表滚动到足够远以将编辑过的项目从屏幕上移开,当我向后滚动再次查看它时,它的值已恢复到我进行更改之前的值,即我的编辑已经丢失.

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, i.e. my edits have been lost.

为了解决这个问题,我在完成编辑之后(以及滚动之前)做了一个 getText 来查看 EditText 中的内容,并且 getText 返回原始文本,即使 EditText 正在显示我的文本.似乎 EditText 只是表面上接受了我的编辑,并且它们还没有绑定到 EditText,这意味着它们在滚出屏幕时会被丢弃.

In trying to sort this out, I've done a getText to look at what's in the EditText after I've done my edits (and before a scroll) and getText returns the original text, even though the EditText is displaying my new text. It seems that the EditText has only accepted my edits superficially and they haven't been bound to the EditText, meaning they get dropped when scrolled off the screen.

谁能告诉我这里发生了什么以及我需要做什么来强制 EditText 保留其编辑?

Can anyone please tell me what's going on here and what I need to do to force the EditText to retain its edits?

推荐答案

但是...如果我将列表滚动到很远足以将编辑过的项目从屏幕,当我回滚看它的价值又回到了在我做出改变之前是什么IE.我的编辑丢失了.

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.

当然.

列表行被回收.您的 Cursor 可能有 1,000 条记录,但如果您滚动列表,则不会创建 1,000 个 EditText 小部件.相反,将有 10 个左右,具体取决于同时可见的行数.行被回收,绑定操作将用来自 Cursor 的新值替换旧的 EditText 值,用于刚刚滚动到屏幕上的任何行,替换之前的任何内容(来自数据库的先前值或用户编辑的值).

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

而且,由于常规的 Cursor 是不可变的,因此您无法以透明的方式将任何编辑保留回列表中.

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.

我怀疑可以创建一个 ListView 行是 EditTexts,可能是通过创建一个自定义的 Adapter 类并处理所有排回收自己.但是,这将是相当多的工作,而且内置类只会为您提供对这种模式的一点支持.

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.

这篇关于滚动列表中的 EditText 项目在滚动离开屏幕时会丢失其更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆