任务:我想显示ListView列表,当你选择一个项目来显示对话列表并将其显示在另一个元素列表中(父元素和子元素)
static class GroupCursorLoader extends CursorLoader {
DB db;
public GroupCursorLoader(Context context, DB db) {
super(context);
this.db = db;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = db.getGroupAll();
return cursor;
}
}
static class DetailCursorLoader extends CursorLoader{
DB db;
public DetailCursorLoader(Context context, DB db) {
super(context);
this.db = db;
}
@Override
public Cursor loadInBackground() {
Cursor cursor;
cursor = db.getDetailAll();
return cursor;
}
}
现在,问题是,如何处理两个Cursor?,即。用以下方法编写
public Loader onCreateLoader(int id,Bundle args)
public void onLoadFinished(Loader loader,Cursor data)
public void onLoaderReset(Loader loader)
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return return new GroupCursorLoader(this, mDB);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
scAdapterForGroup.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d(LOG, "onLoadReset");
}
答案 0 :(得分:0)
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if(id == GROUP_LOADER_ID) {
return new GroupCursorLoader(this, mDB);
} else {
return new DetailCursorLoader(this, mDB);
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if(loader instanceof GroupCursorLoader) {
scAdapterForGroup.swapCursor(data);
} else {
scAdapterForDetails.swapCursor(data);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d(LOG, "onLoadReset");
}