Android:替换整个数据库时游标重新查询

发布于 2024-10-11 03:42:26 字数 622 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2024-10-18 03:42:26

通过丢弃游标,关闭并重新打开数据库,然后重新创建游标来解决。我使用观察者来通知数据库更改的活动,如下所示:

@Override 
public void update(Observable observable, Object data) { 
    // database changed 
    runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
            stopManagingCursor(cursor); 
            dbAdapter.close(); 
            dbAdapter.open(); 
            cursor = getAccounts(); 
            startManagingCursor(cursor); 
            adapter.changeCursor(cursor); 
        } 
    }); 
} 

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:

@Override 
public void update(Observable observable, Object data) { 
    // database changed 
    runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
            stopManagingCursor(cursor); 
            dbAdapter.close(); 
            dbAdapter.open(); 
            cursor = getAccounts(); 
            startManagingCursor(cursor); 
            adapter.changeCursor(cursor); 
        } 
    }); 
} 
a√萤火虫的光℡ 2024-10-18 03:42:26

您是否尝试过从 ListActivity 的 onResume() 方法中调用 SimpleCursorAdapter 上的 notificationDataSetChanged() ?

Have you tried calling notifyDataSetChanged() on your SimpleCursorAdapter from within your ListActivity's onResume() method?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文