我正在使用两个SimpleCursorAdapters在Listview中连续显示文本和图像。问题是,当两次调用这些适配器时,它们会相互冲突。最后,我的Listview仅显示我最后调用的SimpleCursorAdapter的数据。

我需要做的是合并这两个SimpleCursorAdapters,尽管它们使用不同的sql数据库。

有什么解决办法吗?

/** Called when the activity is first created. */
@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);
}

最佳答案

您可以使用MergeCursor类将多个单独的游标公开为单个游标。当您的适配器绑定不同的列->小部件时,您可能必须编写自己的SimpleCursorAdapter子类(或只是CursorAdapter),以便可以根据行进行正确的绑定类型。

07-26 03:28