更新 SimpleCursorAdapter,同时保持 ListView 中的滚动位置

发布于 2025-01-04 13:39:20 字数 950 浏览 1 评论 0原文

我的问题:每当我通过其(自定义)SimpleCursorAdapter 更新其内容时,我的 ListView 都会将其滚动位置重置到顶部。我希望 ListView 在更新时保持其滚动位置。

我首先每次创建一个新的适配器实例并使用 ListView.setAdapter(),但根据 此讨论 我应该更新适配器而不是重置它。我找不到一种方法可以使 SimpleCursorAdapter 正常工作。

以下是我尝试过的一些方法,省略了方法参数:
- 使用新光标的 SimpleCursorAdapter.changeCursorAndColumns() 成功更新了适配器,但仍然重置了滚动位置。
- SimpleCursorAdapter.changeCursor() 的行为方式相同。
- SimpleCursorAdapter.swapCursor() 在 api 11 之前不可用,我正在为 api 8 进行开发。
- SimpleCursorAdapter.notifyDataSetChanged() 不会更新 ListView。 (也许我还缺少另一个步骤。)
- ListView.invalidate() 不会更新 ListView。
- SimpleCursorAdapter.requery() 使 ListView 为空,已被弃用。
- 我熟悉 ListView.setSelection() 解决方案,但我不希望我的 ListView 在更新时根本移动。这会将其捕捉到列表中的某个位置。

这看起来应该是一个常见且简单的问题。有人处理过吗?

My problem: My ListView resets its scroll position to the top whenever I update its contents through its (customized) SimpleCursorAdapter. I would like the ListView to maintain its scroll position when updated.

I started out by creating a new adapter instance every time and using ListView.setAdapter(), but according to this discussion I should be updating the adapter instead of resetting it. I can't find a way to do this for SimpleCursorAdapter that works correctly.

Here are some things I've tried, omitting the method arguments:
- SimpleCursorAdapter.changeCursorAndColumns() with a fresh cursor successfully updates the adapter but still resets the scroll position.
- SimpleCursorAdapter.changeCursor() acts the same way.
- SimpleCursorAdapter.swapCursor() isn't available until api 11, and I am developing for api 8.
- SimpleCursorAdapter.notifyDataSetChanged() does not update the ListView. (Maybe there is another step I'm missing.)
- ListView.invalidate() does not update the ListView.
- SimpleCursorAdapter.requery() makes the ListView blank, and is deprecated.
- I'm familiar with the ListView.setSelection() solution, but I don't want my ListView to move at all when it is updated. This snaps it to a position in the list.

This seems like it should be a common and simple problem. Has anybody dealt with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

虚拟世界 2025-01-11 13:39:20

我遇到了类似的问题:只要光标的内容发生变化,我的 CursorAdapter 就会重置滚动位置。

我没有意识到每次数据更改时我实际上都设置了一个新的列表适配器。我认为光标本身会通知适配器其内容的更改,但在我的情况下,是 ContentProvider 触发 ListFragment 中的 LoaderManager.LoaderCallbacks.onLoadFinished() 。在该回调中,我现在使用 CursorAdapter.changeCursor() 而不是创建新适配器,一切正常。

我希望这有助于解决您的问题。

I've had a similar problem: My CursorAdapter reset the scroll position whenever the contents of the cursor changed.

I didn't realize that I actually did set a new list adapter each time the data changed. I thought the cursor himself would notify the adapter about changes to it's content but in my case it is the ContentProvider that triggers LoaderManager.LoaderCallbacks.onLoadFinished() in my ListFragment. In that callback I now use CursorAdapter.changeCursor() instead of creating a new adapter and everything works just fine.

I hope this helps solving your problem.

一百个冬季 2025-01-11 13:39:20

在异步任务的执行后执行中使用以下代码是否对我有用,

 Cursor cursor = mDataHelper.getWritableDatabase().query(
                        DataContract.Incares.TABLE_NAME,  // Table to Query
                        null, // all columns
                        null, // Columns for the "where" clause
                        null, // Values for the "where" clause
                        null, // columns to group by
                        null, // columns to filter by row groups
                        DataContract.Incares.UUID +" DESC" // sort order
                );
CursorAdapter.changeCursor(cursor);
CursorAdapter.notifyDataSetChanged();

CursorAdapter 是全局变量

Did the same in post execute of Async Task used following code worked for me

 Cursor cursor = mDataHelper.getWritableDatabase().query(
                        DataContract.Incares.TABLE_NAME,  // Table to Query
                        null, // all columns
                        null, // Columns for the "where" clause
                        null, // Values for the "where" clause
                        null, // columns to group by
                        null, // columns to filter by row groups
                        DataContract.Incares.UUID +" DESC" // sort order
                );
CursorAdapter.changeCursor(cursor);
CursorAdapter.notifyDataSetChanged();

with CursorAdapter being a global variable

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