【问题标题】:Android CursorIndexOutOfBoundsException Index 0 requested, with a size of 0Android CursorIndexOutOfBoundsException 请求索引 0,大小为 0
【发布时间】:2013-01-10 18:11:10
【问题描述】:

我正在尝试在我的 Activity 中使用 2 个不同的 ListView 来填充数据库内容 onCreate。自从我添加代码以来,我现在都因标题中提到的错误而受到逼迫。

我不知道为什么会这样。

代码如下:

    currentList = (ListView)findViewById(R.id.list_current_day);
    cursor = datasource.fetchCurrentLogs(dayExerciseDataID);
    to = new int[] { R.id.listitem_log_reps, R.id.listitem_log_weight };
    dataAdapterCurrent = new SimpleCursorAdapter(this, R.layout.listitem_log, cursor, columns, to, 0);
    currentList.setAdapter(dataAdapterCurrent);

    previousList = (ListView)findViewById(R.id.list_previous_day);
    cursor = datasource.fetchPreviousLogs(dayExerciseDataID);
    to = new int[] { R.id.listitem_log_reps, R.id.listitem_log_weight };
    dataAdapterPrevious = new SimpleCursorAdapter(this, R.layout.listitem_log, cursor, columns, to, 0);
    previousList.setAdapter(dataAdapterPrevious);

这是数据库的结构和功能:

public Cursor fetchCurrentLogs(long dayExerciseDataID) {
    // where day = today
    Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_REPS + ", " + MySQLiteHelper.COLUMN_WEIGHT + " " +
            "from " + MySQLiteHelper.TABLE_LOGS + " " +
            "where " + MySQLiteHelper.COLUMN_ID_DAY_EXERCISE + " = '" + dayExerciseDataID + "'", null);
    if (cursor != null) { cursor.moveToFirst(); }
    return cursor;
}

public Cursor fetchPreviousLogs(long dayExerciseDataID) {
    // where day = closest day from today
    Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_REPS + ", " + MySQLiteHelper.COLUMN_WEIGHT + " " +
            "from " + MySQLiteHelper.TABLE_LOGS + " " +
            "where " + MySQLiteHelper.COLUMN_ID_DAY_EXERCISE + " = '" + dayExerciseDataID + "'", null);
    if (cursor != null) { cursor.moveToFirst(); }
    return cursor;
}

    database.execSQL("create table " + TABLE_LOGS + " (" 
            + COLUMN_ID + " integer primary key autoincrement," 
            + COLUMN_ID_DAY_EXERCISE + " integer not null,"
            + COLUMN_REPS + " integer not null,"
            + COLUMN_WEIGHT + " real not null,"
            + COLUMN_1RM + " real not null,"
            + COLUMN_DATE + " integer not null"
            + ")");

有人能解释一下为什么会这样吗?如果我错过了任何重要的代码,请告诉我,我会更新问题。

【问题讨论】:

    标签: android indexing cursor indexoutofboundsexception


    【解决方案1】:

    你必须以这种方式使用 SimpleCursorAdapter

    dataAdapterPrevious = new SimpleCursorAdapter(this, R.layout.listitem_log, cursor, columns, to);
    

    而不是这个

     dataAdapterPrevious = new SimpleCursorAdapter(this, R.layout.listitem_log, cursor, columns, to, 0);
    

    【讨论】:

    • 不,that constructor 已弃用,只能在 API 10 及更低版本上使用。
    【解决方案2】:

    尝试为两个列表使用不同的cursor 实例。

    currentList = (ListView)findViewById(R.id.list_current_day);
    cursor1 = datasource.fetchCurrentLogs(dayExerciseDataID);
    

    previousList = (ListView)findViewById(R.id.list_previous_day);
    cursor2 = datasource.fetchPreviousLogs(dayExerciseDataID);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      相关资源
      最近更新 更多