如何使用新的Loader API时,保持ListView控件的位置?如何使用、控件、位置、API

2023-09-05 06:16:45 作者:我是之一还是唯一

在蜂窝的装载机的API被引入适当的方式提供数据的应用程序通过执行繁重的后台线程。在我的应用我的工作,以取代我的所有光标 s的装载机 s表示返回光标秒。由于 Cursor.requery()现在pciated德$ P $,建议只需要调用 restartLoader ,并允许工作再次在后台线程完成,然后 changeCursor ,当它在 onLoadFinished 返回。

In Honeycomb the Loader APIs were introduced as the proper way to provide data to an application by doing the heavy lifting on a background thread. In my application I'm working to replace all my Cursors with Loaders that return Cursors. Since Cursor.requery() is depreciated now, it is recommended to just call restartLoader and allow the work to again be done on a background thread and then changeCursor when it returns in onLoadFinished.

所有这些奇妙的作品不同的是ListView控件不保持它的滚动位置时,我想重新查询数据,使用 Cursor.requery()这个曾经工作因为它是与更新的数据相同的光标实例。

All of this works wonderfully except that the ListView doesn't maintain it's scroll position when I want to requery the data, using Cursor.requery() this used to work because it was the same Cursor instance with updated data.

我怎么能刷新我的装载机没有松动的滚动位置?

How can I refresh my Loader without loosing the scroll position?

推荐答案

装载机类本身不知道什么时候该数据集的变化,并具体落实到 CursorLoader 当前。现在,我的应用程序里面,我需要刷新数据是本地SQLite数据库(无ContentProvider的)里面,所以我不得不修改 CursorLoader 使用我的本地数据库,而不是作为戴安娜Hackborne在developer组。

The Loader class by itself doesn't know when the dataset changes and is implementation specific to CursorLoader currently. Now inside my application the data I needed to refresh is inside a local SQLite database (no ContentProvider) so I had to modify the CursorLoader to use my local database instead, as Dianne Hackborne mentioned on the developer group.

不幸的是刚刚返回从loadInBackground游标不允许 ContentObserver 正确通知程序的时候,数据的变化,这是因为 AbstractCursor 只要求 notifyChanged()重新查询()被称为上的光标。

Unfortunately just returning a Cursor from loadInBackground doesn't allow the ContentObserver to properly notify the Loader when the data changes, this is because AbstractCursor only calls notifyChanged() when requery() is called on the Cursor.

由于 ContentObserver 将永远不会通知装载机的数据发生了变化,我最终调用 Loader.onContentChanged()自己是必要的。在这一点上一切都似乎是工作,但如果有显著问题arrise我会更新这个答案。

Since the ContentObserver will never notify the Loader that the data has changed, I ended up calling Loader.onContentChanged() myself as needed. At this point it everything seems to be working but I'll update this answer if any significant issues arrise.

 
精彩推荐