数据更改后 CursorLoader 不更新

发布于 2024-12-11 22:47:37 字数 394 浏览 0 评论 0原文

我创建了一个小型应用程序,试图了解 LoaderManager 和 CursorLoader 类的功能。

我已经在我的 FragmentActivity 类上实现了 LoaderCallbacks ,一切正常,除了当我通过 ContentResolver.update()< 更新数据时/code> 或 ContentResolver.insert()-方法,onLoadFinished() 未调用,因此我的数据不会更新。

我有一个自定义的 ContentProvider,我想知道问题是否出在我的 ContentProvider 中,没有通知数据更改或其他原因。

I have created a small application, trying to understand the functionality of the LoaderManager and CursorLoader-classes.

I have implemented LoaderCallbacks<Cursor> on my FragmentActivity-class and everything works fine, except the fact that when I update my data via ContentResolver.update() or ContentResolver.insert()-methods, onLoadFinished() is not called and as a result my data doesn't update.

I have a custom ContentProvider and I am wondering if the problem is in my ContentProvider not notifying that the data changed or something else.

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

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

发布评论

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

评论(3

半衾梦 2024-12-18 22:47:38

您是否在 Cursor 上调用了 setNotificationUri(ContentResolver cr, Uri uri),然后才在 ContentProvider.query() 中返回它?

您是否在 ContentProvider 的“insert”方法中调用了 getContext().getContentResolver().notifyChange(uri, null)

编辑:

要获取 ContentResolver,请在 ContentProvider 中调用 getContext().getContentResolver()

Did you call setNotificationUri(ContentResolver cr, Uri uri) on the Cursor before returning it in ContentProvider.query()?

And did you call getContext().getContentResolver().notifyChange(uri, null) in the 'insert' method of your ContentProvider?

EDIT:

To get a ContentResolver call getContext().getContentResolver() in your ContentProvider.

祁梦 2024-12-18 22:47:38

还要检查您是否在某处调用了cursor.close(),因为在这种情况下您取消注册了由CursorLoader注册的内容观察器。游标的关闭由CursorLoader 管理。

Also check if you call somewhere cursor.close(), because in this case you unregister the content observer which was registered by CursorLoader. And the cursor closing is managed by CursorLoader.

擦肩而过的背影 2024-12-18 22:47:38

接受的答案有点难以理解,因此我编写答案是为了方便其他开发人员。

  1. 的类
  2. 转到您扩展了 ContentProvider

    Find the query() 具有以下语法的方法

    public Cursor query(Uri uri, String[]projection,Stringselection,String[]selectionArgs,StringsortOrder)

  3. 在要返回光标的位置写入此行

    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    returncursor;

最后我的查询方法是这样的

@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    Cursor cursor;
    cursor = noticeDbHelper.getReadableDatabase().query(
            NoticeContract.NoticeTable.TABLE_NAME,
            projection,
            selection,
            selectionArgs,
            null,
            null,
            sortOrder
    );
    //This line will let CursorLoader know about any data change on "uri" , So that data will be reloaded to CursorLoader
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}`

Accepted answer was the little bit tricky to understand so I am writing the answer to make it easy for other developers..

  1. Go to the class in which you have extended the ContentProvider
  2. Find the query() method which has the following syntax

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

  3. Write this line where you are returning the cursor

    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;

In the end, my query method looks like this

@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    Cursor cursor;
    cursor = noticeDbHelper.getReadableDatabase().query(
            NoticeContract.NoticeTable.TABLE_NAME,
            projection,
            selection,
            selectionArgs,
            null,
            null,
            sortOrder
    );
    //This line will let CursorLoader know about any data change on "uri" , So that data will be reloaded to CursorLoader
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文