2つのローダーがあります。1つはバインドされた戻りデータを2TextViews
に入力し、もう1つはを入力しListView
ます。状況ごとに正しいローダーがロードされることを確認するにはどうすればよいですか?最初のローダー(WR_VIEW
ケース)が作成またはロードされていないように見える場所でエラーが発生してonLoadFinished()
いるため、その列を呼び出さない2番目のローダーにアクセスしているため、「そのような列が見つかりませんエラー」が返されます。
ここで私のonCreate
方法では、リストビュー用のアダプタを設定しました。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workouts_options);
String[] uiBindFrom = { ExerciseTable.COLUMN_NAME };
int[] uiBindTo = { R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(),
R.layout.exercises_row,
null,
uiBindFrom,
uiBindTo,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
final ListView lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setEmptyView(findViewById(android.R.id.empty));
lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
}
});
getSupportLoaderManager().initLoader(WR_VIEW, null, this);
getSupportLoaderManager().initLoader(EXERCISE_VIEW, null, this);
}
私の2つの異なるを作成しますCursorLoader
:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch(id) {
case WR_VIEW:
String[] projection = { WorkoutRoutineTable.COLUMN_ID, WorkoutRoutineTable.COLUMN_NAME, WorkoutRoutineTable.COLUMN_DESCRIPTION };
return new CursorLoader(this, Workouts.buildWorkoutIdUri(""+mRowId), projection, null, null, null);
default:
String[] columns = {ExercisesColumns.NAME, WRExercisesColumns.COLUMN_ID };
return new CursorLoader(this, Workouts.buildWorkoutIdExerciseUri(""+mRowId), columns, null, null, null);
}
}
TextViews
私のここと:swapCursor
のためにデータをバインドしますListView
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
int id = loader.getId();
switch(id) {
case WR_VIEW:
if (cursor != null && cursor.moveToFirst()) {
mNameText.setText(cursor.getString(cursor.getColumnIndexOrThrow(WorkoutRoutineTable.COLUMN_NAME)));
mDescriptionText.setText(cursor.getString(cursor.getColumnIndexOrThrow(WorkoutRoutineTable.COLUMN_DESCRIPTION)));
getSupportActionBar().setTitle(mNameText.getText());
if (!TextUtils.isEmpty(mDescriptionText.getText())) {
getSupportActionBar().setSubtitle(mDescriptionText.getText());
}
cursor.close();
}
default:
adapter.swapCursor(cursor);
}
}
ローダーのリセット:
public void onLoaderReset(Loader<Cursor> loader) {
int id = loader.getId();
switch(id) {
case WR_VIEW:
break;
default:
adapter.swapCursor(null);
break;
}
}