【问题标题】:Why does my SimpleCursorAdapter override my other SimpleCursorAdapter?为什么我的 SimpleCursorAdapter 会覆盖我的其他 SimpleCursorAdapter?
【发布时间】:2012-06-24 07:59:42
【问题描述】:

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

【问题讨论】:

  • 你的行是如何实现的?当您将列表适配器设置为图像时,它不再设置为提醒(并且图像显然没有提醒数据,因此没有文本)
  • 图像和文本是否存储在数据库的不同表中?
  • 是的,它们存储在不同的数据库中。有什么解决办法吗??

标签: android sqlite simplecursoradapter android-cursor


【解决方案1】:

为什么我的SimpleCursorAdapter 会覆盖我的另一个SimpleCursorAdapter

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

我做错了什么?

您的代码无法正常工作的原因是您调用了两次setListAdapter。对setListAdapater 的第二次调用取消绑定第一个适配器,然后将第二个适配器绑定到您的ListView,从而使您的第一次调用完全无用。您的ListActivityListView 只能有一个适配器(因此您需要以某种方式合并两个适配器的实现)。

【讨论】:

  • 问题是我使用了两个 SQl 数据库。因此我需要这两个 SimpleCursorAdapter。有什么办法可以解决这个问题???
  • SQL 允许您使用JOIN 子句在单个语句中查询两个表。也许this 会让你开始。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2021-04-13
    • 2012-02-05
    • 2012-10-15
    • 2020-04-01
    相关资源
    最近更新 更多