gpt4 book ai didi

android - 基础数据更改时 Cursorloader 不刷新

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:00:33 24 4
gpt4 key购买 nike

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

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

正如我在写这篇文章时所建议的那样,关于这个问题有很多问题。例如 CursorLoader not updating after data change

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

  • 在您的内容提供程序 query() 方法中,您需要告诉游标有关通知的信息

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

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

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

我正在做那些事情。

我也不会关闭我返回的任何游标。

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

我已经在加载器管理器中打开了调试,并且可以看到在数据更改后我强制刷新的位置(即加载器被重新启动并且之前被标记为不活动),但是没有任何说明任何事情都在自动发生 - 相反,响应我的强制刷新。

为什么我的装载机没有被刷新有什么想法吗?

-- 编辑--

加载程序创建:

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

其中 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();

加载程序重置清除表。

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();

内容提供者做:

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;
}

然后插入/更新类似:

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);
}

最佳答案

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

在 onLoadFinished 中,调用如下内容:

mAdapter.swapCursor(cursor)

在 onLoaderReset 中,调用它来删除对旧游标的引用:

mAdapter.swapCursor(null)

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

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

关于android - 基础数据更改时 Cursorloader 不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17075382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com