Android的SimpleCursorAdapter不更新时,数据库更改数据库、Android、SimpleCursorAdapter

2023-09-12 21:39:23 作者:毒药。

我有一个Android ListActivity 由数据库光标通过 SimpleCursorAdapter支持

I have an Android ListActivity that is backed by a database Cursor through a SimpleCursorAdapter.

当的项被点击时,coresponding行中的数据库中的标志字段被触发并且在列表视图需要更新。

When the items are clicked, a flag field in the coresponding row in the database is toggled and the view in the list needs to be updated.

的问题是,当这样更新视图熄灭屏幕和再循环,旧值被显示在视图当它返回到视图中。同样的事情发生,只要THR名单redrawb(方向的变化,等等)。

The problem is, when the view that's updated goes off screen and is recycled, the old value is displayed on the view when it returns into view. The same thing happens whenever thr list is redrawb (orientation changes, etc).

我用 notifydatasetchanged()刷新光标适配器,但似乎效果不佳。

I use notifydatasetchanged() to refresh the cursor adapter but it seems ineffective.

应该如何更新数据库,使光标更新呢?

How should I be updating the database so the cursor is updated as well?

推荐答案

呼叫重新查询()光标当你改变,你要反映在光标数据库中的数据(或事物光标填充,像的ListView 通过的CursorAdapter )。

Call requery() on the Cursor when you change data in the database that you want reflected in that Cursor (or things the Cursor populates, like a ListView via a CursorAdapter).

A 光标类似于一个ODBC客户端游标 - 它拥有所有的查询结果psented数据再$ P $的。因此,仅仅因为你改变数据库中的数据时,光标不知道这些变化,除非你通过刷新重新查询()

A Cursor is akin to an ODBC client-side cursor -- it holds all of the data represented by the query result. Hence, just because you change the data in the database, the Cursor will not know about those changes unless you refresh it via requery().

更新:这整个问题并设置答案应该因年老被删除,但是这显然是不可能的。任何人士Android的答案应该记住,Android是一个快速移动的目标,从2009年起的答案通常不如有新的答案。

UPDATE: This whole question and set of answers should be deleted due to old age, but that's apparently impossible. Anyone seeking Android answers should bear in mind that the Android is a swiftly-moving target, and answers from 2009 are typically worse than are newer answers.

目前的解决方案是获得一个新的光标并使用 changeCursor() swapCursor()的CursorAdapter 来影响数据的变化。

The current solution is to obtain a fresh Cursor and use either changeCursor() or swapCursor() on the CursorAdapter to affect a data change.