我正在创建一个 MergeCursor 像这样:
@Override
public Cursor loadInBackground() {
Log.d(LOG, "loading data in background");
ContentResolver cr = getContext().getContentResolver();
Cursor people = getContext().getContentResolver().query(PeopleProvider.CONTENT_URI, null, null, null, null);
people.setNotificationUri(cr, PeopleProvider.CONTENT_URI);
Cursor jobs = getContext().getContentResolver().query(JobProvider.CONTENT_URI, null, null, null, null);
jobs.setNotificationUri(cr, JobsProvider.CONTENT_URI);
Cursor horses = getContext().getContentResolver().query(HorseProvider.CONTENT_URI, null, null, null, null);
horses.setNotificationUri(cr, HorseProvider.CONTENT_URI);
Cursor[] c = new Cursor[3];
c[0] = people;
c[1] = jobs;
c[2] = horses;
MergeCursor mc = new MergeCursor(c);
return mc;
}
然后当我向相应的数据库表添加新行时:
"PeopleProvider.java"
@Override
public Uri insert(Uri uri, ContentValues values) {
Log.d(LOG, "Insert into People");
long id = database.insertOrThrow(DatabaseConstants.TABLE_PEOPLE, null, values);
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, id);
}
我也试过:
"WhereDataIsPresentedFragment.java"
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(LOG, "onActivityResult()");
switch(requestCode) {
case NEW_PEOPLE_ACTIVITY:
Log.d(LOG, "Returned with newly added person");
if (resultCode==Activity.RESULT_OK) {
mListViewAdapter.notifyDataSetChanged();
getActivity().getContentResolver().notifyChange(PeopleProvider.CONTENT_URI, null);
}
break;
}
}
所有方法都被调用(通过 Log.d 确认),所以我不确定为什么在触发通知时没有更新游标。任何对制作这项工作的见解将不胜感激。过去我对常规游标没有任何问题,但这个 MergeCursor 业务没有运气。