我正在创建一个像这样的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业务没有运气。
答案 0 :(得分:0)
所以我使用它的方式(无论它是否正确)是通过进一步继承我的AsyncTaskLoader类:
@Override
protected void onStartLoading() {
forceLoad();
}
基本上,通过将forceLoad();
添加到onStartLoading()
方法,现在一切都按预期工作。我能够删除对notifyDataSetChanged()
的所有额外调用以及对URI的通知。那一行修好了一切。如果有人有更好的解决方案请告诉我,但现在这就是我得到的。