在notifyChange调用之后,CursorLoader不会更新

时间:2014-01-22 04:00:56

标签: android

我已注册CursorLoader,但未收到ContentProvider

的更新

事件的顺序是:

  1. Fragment中,将CursorLoader注册为:

    getLoaderManager().initLoader(LOADER_FAVS_ID, null, this);
    

    注意我使用的是支持库版本,因此此方法为android.support.v4.app.Fragment.getLoaderManager()

  2. CursorLoader已注册,Cursor中已加载onLoadFinished

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            Log.i(TAG, "onLoadFinished");
            switch (loader.getId()) {
            case LOADER_FAVS_ID:
                    Log.i(TAG,
                                    "cursor notification uri: " + cursor.getNotificationUri());
                    mCursorAdapter.swapCursor(cursor);
                    break;
            }
    }
    

    例如,哪个日志cursor notification uri: content://com.myapp.mylocation/db_locations。这是因为我确保在从ContentProvider返回Cursor之前调用cursor.setNotificationUri(getContext().getContentResolver(), uri);。 另请注意,我的内容提供商会返回MergeCursor

  3. 一段时间后,我update()打电话给ContentProvider,并执行以下行:

    Log.i(TAG,
            "Notifying uri: " + uri.toString());
    getContext().getContentResolver().notifyChange(
            uri, null);
    

    哪个记录Notifying loc uri: content://com.myapp.mylocation/db_locations,与上面的uri相同。

  4. 但永远不会调用onLoadFinished,我的Cursor永远不会更新。我相信我已经遵循了我能找到的建议,所有建议基本上都是this。为什么在所有这些之后不会调用onLoadFinished

1 个答案:

答案 0 :(得分:5)

解决了它,但由于我没有看到这个记录,这是我的解决方案。

我从我的MergeCursor返回ContentProvider,基本上只是连接了一个游标列表。我有

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {
            // Generate an array of Cursors

            MergeCursor mergCursor = new MergeCursor(cursorArray);

            // notify potential listeners
            mergCursor.setNotificationUri(getContext().getContentResolver(), uri);

            return mergCursor;
    }

我的CursorLoader从未收到通知。但是,由于MergeCursor基本上只是Cursor的数组,您需要在MergeCursor每个光标上设置通知uri。

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {
            // Generate an array of Cursors

            // set the notification uris...
            for (Cursor cursor : cursorArray) {
                    cursor.setNotificationUri(getContext().getContentResolver(), uri);
            }

            MergeCursor mergCursor = new MergeCursor(cursorArray);

            return mergCursor;
    }

现在一切都按预期工作了!