【问题标题】:CursorLoader, unsubscribe from ContentProvider notificationsCursorLoader,取消订阅 ContentProvider 通知
【发布时间】:2015-03-11 21:39:18
【问题描述】:

我正在使用CursorLoader 查询ContentProvider 以填充ListView,并使用以下代码(包装在LoaderManager 中):

CursorLoader loader = new CursorLoader(getActivity(),
                Provider.CONTENT_SOME_URI,
                projection,
                selection,
                null,
                null);

还有一个AsyncQueryHandler 来更新给定_idSOME_VALUE,在点击CheckBox 时触发

final AsyncQueryHandler handler = new AsyncQueryHandler(contentResolver) {};
final ContentValues values = new ContentValues();
                    values.put(Table.SOME_COLUMN, someValue);
final Uri uri = Uri.withAppendedPath(Provider.CONTENT_SOME_URI, String.valueOf(someId));
handler.startUpdate(0,
       null,
       uri,
       values,
       null,
       null);

最后,在我的 ContentProvider 中,我有类似的东西:

@Override
public int update(Uri uri, ...){
    ... // update stuff
    getContext().getContentResolver().notifyChange(uri, null);
}

问题如下:

当我检查单个项目时,该项目被更新,notifyChange 告诉CursorLoader 重新加载,整个列表被重新加载,导致列表滚动到顶部,动画停止,每次检查CheckBox。 ..

不知道怎么办……

谢谢!

【问题讨论】:

  • 如果整个列表正在重新加载,那么您的onLoadFinished 做错了。您可以包含该代码吗?
  • 感谢@ianhanniballake,我在 onLoadFinished 中调用 setListAdapter,改为使用 swapCursor(),太棒了!也许你可以创建一个答案,所以我可以关闭它。

标签: java android cursor android-contentprovider android-cursorloader


【解决方案1】:

当您调用setListAdapter 时,这会导致列表完全清除其数据并重新生成整个列表。相反,您应该只在 onLoadFinished() 中调用 swapCursor() - 这会告诉适配器数据已更改,但它将使用列表元素 ID 重用项目,从而使您的列表在您的情况下保持完全稳定

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多