【发布时间】:2022-02-10 23:08:35
【问题描述】:
我创建了一个简单的Activity,它实现了LoaderManager.LoaderCallbacks<Cursor> 接口。
OnCreateLoader() 很简单:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
Log.d(TAG, "On create loader");
Uri queryUri = ContentUris.withAppendedId(SmartPresentationMessage.Person.CONTENT_URI, 1);
CursorLoader cursorLoader = new CursorLoader(this, queryUri, null, null, null, null);
return cursorLoader;
}
当我在创建Activity 时调用getLoaderManager().initLoader(0, null, this) 时会调用此方法。
我的问题是在我的ContentProvider 上,它有以下query() 方法:
@Override
public Cursor query(Uri uri, String[] projection, String where,
String[] whereArgs, String sortOrder)
{
int match = sUriMatcher.match(uri);
Cursor queryCursor;
SQLiteDatabase mdb = mOpenHelper.getReadableDatabase();
switch (match)
{
case PERSON:
long personID = ContentUris.parseId(uri);
queryCursor = mdb.query(TABLE_NAME, projection,
SmartPresentationMessage.Person._ID + " = " + personID,
whereArgs, null, null, sortOrder);
asyncQueryRequest("" + (taskTag ++ ) , QUERY_URI + "/" + "person");
return queryCursor;
default:
throw new IllegalArgumentException("unsupported uri: " + uri);
}
}
在收到来自网络服务器的响应后,queryCursor 在数据库中得到更新。
但是,当我在ContentProvider的更新方法中调用getContext().getContentResolver().notifyChange(uri, null)方法时,
我在 Activity 上的 OnLoadFinished() 方法未被调用,即使 uri 与实例化 CursorLoader 时使用的 uri 相同。
这是ContentProvider上的更新方法:
@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs)
{
//getContext().getContentResolver().notifyChange(uri, null);
// insert the initialValues into a new database row
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int affected;
try
{
switch (sUriMatcher.match(uri))
{
case PERSON:
long personID = ContentUris.parseId(uri);;
affected = db.update(TABLE_NAME, values,
SmartPresentationMessage.Person._ID + " = " + personID,
whereArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
finally
{
db.close();
}
return affected;
}
有人可以告诉我,在同一个 uri 上调用 notifyChange() 时,我的 OnLoadFinshed() 方法没有被调用的原因是什么?
谢谢你, 克里斯蒂
【问题讨论】:
标签: android android-contentprovider android-cursor android-cursorloader