使用LoaderManager的多个装载程序,而不是正确的装载程序

时间:2012-07-13 02:24:06

标签: android android-loadermanager android-loader

我有两个加载器,一个用于将绑定返回的数据填充到2 TextViews,另一个用于填充ListView。如何确保为每种情况加载正确的装载程序?我在第一个加载器(WR_VIEW case)似乎没有创建或加载的位置出现错误,因此在onLoadFinished()中它返回“No such column found error”,因为它正在访问第二个加载器,不会调用该列。

在我的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;
    }
}

1 个答案:

答案 0 :(得分:5)

在你的onLoadFinished()中,在WR_VIEW案例之后你没有break,所以默认子句也会运行,将错误的光标传递给你的适配器。 (onCreateLoader似乎也有类似的问题)