为什么我的SimpleCursorAdapter覆盖了我的其他SimpleCursorAdapter?

时间:2012-06-01 17:10:33

标签: android sqlite simplecursoradapter android-cursor

在我的onCreate方法中,我调用其他方法,即fillData()和fillImages。 fillData的作用是,它使用文本填充Listview中的一行,fillImages将图像放入行中。到现在为止还挺好。 显然当我只在我的onCreate方法中调用fillData时,只显示文本。当我打电话给fillImages时也是如此。

问题是,当我同时调用它们的内容时,我最后调用的方法显示出来。 示例:当我这样称呼时:

@Override
public void onCreate() {
    //Here is some content left away that is not important.
    fillData();
    fillImages()
}

我只获得了fillImages()方法的内容。

我做错了什么?下面是我的onCreate(),fillData()和fillImages()方法的代码。

更新:我如何解决这个问题?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reminder_list);
    mDbHelper = new RemindersDbAdapter(this);
    mImageHelper = new ImageAdapter(this);
    mDbHelper.open();
    mImageHelper.open();
    fillData();
    fillImages();
    registerForContextMenu(getListView());
}

//
// Fills the ListView with the data from the SQLite Database.
//
private void fillData() {
    Cursor remindersCursor = mDbHelper.fetchAllReminders();
    startManagingCursor(remindersCursor);

    // Creates an array with the task title.
    String[] from = new String[] {RemindersDbAdapter.KEY_TITLE, RemindersDbAdapter.KEY_BODY};

    // Creates an array for the text.
    int[] to = new int[] {R.id.text1, R.id.text2};

    // SimpleCursorAdapter which is displayed.
    SimpleCursorAdapter reminders = new SimpleCursorAdapter(this, R.layout.reminder_row, remindersCursor, from, to);
    setListAdapter(reminders);

}

//
// Fills the ListView with the images from the SQLite Database.
//
private void fillImages() {
    Cursor imageCursor = mImageHelper.fetchAllImages();
    startManagingCursor(imageCursor);

    // Creates an array with the image path.
    String[] fromImage = new String[] {ImageAdapter.KEY_IMAGE};

    // Creates an array for the text.
    int[] toImage = new int[] {R.id.icon};

    // SimpleCursorAdapter which is displayed.
    SimpleCursorAdapter images = new SimpleCursorAdapter(this, R.layout.reminder_row, imageCursor, fromImage, toImage);
    setListAdapter(images);
}

2 个答案:

答案 0 :(得分:2)

  

为什么我的SimpleCursorAdapter会覆盖我的其他SimpleCursorAdapter

您错误地使用了override这个词。 方法重写是指子类提供其超类中提供的方法的特定实现。这与您遇到的问题完全无关。

  

我做错了什么?

您的代码无效的原因是您要两次调用setListAdapter。第二次调用setListAdapater取消绑定第一个适配器,然后将第二个适配器绑定到ListView,从而使您的第一个调用完全没用。你的ListActivity ListView只能有一个适配器(所以你需要以某种方式合并你的两个适配器的实现)。

答案 1 :(得分:1)

使用这两种方法设置了两个setListAdapter,最后一个是setListAdapter(images);,因此列表只设置了最后一个适配器数据...

相关问题