基础数据更改时Cursorloader不刷新

K5 *_*ser 6 android android-cursorloader

我有一个内容提供者,一个内容解析器和一个游标加载器.加载器用于间接填充listview(即不是简单的游标适配器,而是数组适配器,因为我需要使用游标的结果来收集其他数据).

当我更改基础数据时,listview不会重新填充,因为未onLoadFinished(Loader<Cursor>, Cursor)调用回调.

正如我在写这篇文章时所建议的那样,在这个问题上存在很多问题.例如, CursorLoader在数据更改后未更新

所有问题都指出了两件事:

  • 在内容提供程序query()方法中,您需要告诉光标有关通知a'la

c.setNotificationUri(getContext().getContentResolver(), uri);

  • 在内容提供者插入/更新/删除方法中,您需要通知URI:

getContext().getContentResolver().notifyChange(uri, null);

我正在做那些事.

我也没有关闭任何我回来的游标.

请注意,我的内容提供商位于一个单独的应用中(将其视为内容提供商应用 - 没有启动器主要活动).com.example.provider APK,com.example.app通过content://com.example.provider/table URI等调用(在内容解析器中)内容解析器(dbhelper)存在于库项目中(com.example.appdb)活动链接.(这样,多个项目可以通过链接使用dbhelper,所有内容提供者都通过单个APK安装)

我已经在装载程序管理器中启用了调试,并且可以看到在数据更改后强制刷新的位置(即重新启动加载程序并且之前被标记为非活动状态),但没有任何内容自动发生 - 而是响应于我的力量刷新.

我的装载机没有被刷新的任何想法?

- 编辑 -

加载器创建:

Log.d(TAG, "onCreateLoader ID: " + loaderID);
// typically a switch on the loader ID, and then 
return dbHelper.getfoo()
Run Code Online (Sandbox Code Playgroud)

getfoo()通过以下方式返回游标加载器:

return new CursorLoader(context, FOO_URI, foo_Fields, foo_query, foo_argArray, foo_sort );

Loader Finish获取光标并填充表格(并进行一些处理) - 没有什么花哨的.

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    Log.d(TAG, "onLoadFinished id: " + loader.getId());
    // switch on loader ID .. 
    FillTable(cursor, (FooAdapter) foo_info.listview.getAdapter();
Run Code Online (Sandbox Code Playgroud)

Loader Reset清除表格.

public void onLoaderReset(Loader<Cursor> loader) {
    Log.d(TAG, "onLoaderReset id: " + loader.getId());
    // normally a switch on loader ID
    ((FooAdapter)foo_info.listview.getAdapter()).clear();
Run Code Online (Sandbox Code Playgroud)

内容提供商做:

public Cursor query(Uri uri,String [] projection,String selection,String [] selectionArgs,String sortOrder){Cursor cursor; SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Cursor cursor;
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        SQLiteDatabase db = dbHelper.getReadableDatabase();

        switch (sUriMatcher.match(uri)) {
        case FOO:
            qb.setTables(FOO);
            qb.setProjectionMap(FooProjectionMap);
            break; 
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);

        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
}
Run Code Online (Sandbox Code Playgroud)

}

然后插入/更新类似:

public Uri insert(Uri uri, ContentValues initialValues) {
    ContentValues values;
    if (initialValues != null) {
        values = new ContentValues(initialValues);
    } else {
        values = new ContentValues();
    }

    String tableName;
    Uri contentUri;

    switch (sUriMatcher.match(uri)) {
    case FOO:
        tableName = FOOTABLE;
        contentUri = FOO_URI;
        break;

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    long rowId = db.insert(tableName, null, values);
    if (rowId > 0) {
        Uri objUri = ContentUris.withAppendedId(contentUri, rowId);
        getContext().getContentResolver().notifyChange(objUri, null);
        return objUri;
    }

    throw new SQLException("Failed to insert row into " + uri);
}
Run Code Online (Sandbox Code Playgroud)

Afz*_*ivE 2

我发现您没有在 onLoadFinished 和 onLoaderReset 中调用 swapCursor 或 changeCursor 。您需要为适配器执行此操作才能访问加载到新游标中的数据。

在 onLoadFinished 中,调用如下内容:

mAdapter.swapCursor(cursor)
Run Code Online (Sandbox Code Playgroud)

在 onLoaderReset 中,调用此函数来删除对旧光标的引用:

mAdapter.swapCursor(null)
Run Code Online (Sandbox Code Playgroud)

其中 mAdapter 是您的 listView 的适配器。

更多信息:http://developer.android.com/guide/components/loaders.html#onLoadFinished