Android:替换整个数据库时游标重新查询
我有一个 ListActivity,它使用 SimpleCursorAdapter 来显示数据库内容。当底层数据库发生更改时,我在刷新视图时遇到问题。
作为实验,我在活动的 onResume()
中添加了对 Cursor.requery()
的调用,以进行调试,因此我所要做的就是翻转到不同的活动和返回,它应该强制刷新。我还重写了 onContentChanged()
以进行调试。事实证明正在调用 requery()
,但这不会导致调用 onContentChanged()
。
我怀疑游标正在使用陈旧的缓存数据。提示更改的操作实际上涉及擦除和替换整个数据库。我认为我需要扔掉游标并从头开始重建它,而不是简单地重新查询。
我尝试过结合 Cursor.requery() 关闭并重新打开底层数据库,但光标似乎不喜欢像这样将数据库从其脚下拉出来。 (列表最终为空。)
编辑:我还尝试在 onResume()
中调用 adapter.notifyDataSetChanged()
以及 requery()
>,但这没有什么区别。
有什么建议吗?
I have a ListActivity
which uses a SimpleCursorAdapter
to display database content. I am having problems getting the view refreshed when the underlying database changes.
As an experiment, I've added a call to Cursor.requery()
in the activity's onResume()
, for debugging, so all I have to do is flip to a different activity and back and it should force a refresh. I've also overridden onContentChanged()
, for debugging. It transpires that requery()
is being called, but this is not resulting in a call to onContentChanged()
.
I suspect the cursor is using stale cached data. The operation which prompts the change actually involves wiping and replacing the whole database. Rather than simply requerying, I think I need to throw away the cursor and rebuild it from scratch.
I have tried closing and reopening the underlying database, in conjunction with Cursor.requery()
, but the cursor doesn't seem to like having the db pulled from under its feet like this. (The list ends up empty.)
Edit: I've also tried calling adapter.notifyDataSetChanged()
alongside the requery()
in onResume()
, but it makes no difference.
Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过丢弃游标,关闭并重新打开数据库,然后重新创建游标来解决。我使用观察者来通知数据库更改的活动,如下所示:
Solved by discarding the cursor, closing and reopening the database, and then recreating the cursor. I use an Observer to inform the activity of the database change, as follows:
您是否尝试过从 ListActivity 的 onResume() 方法中调用 SimpleCursorAdapter 上的 notificationDataSetChanged() ?
Have you tried calling notifyDataSetChanged() on your SimpleCursorAdapter from within your ListActivity's onResume() method?