Search code examples
androidindexingcursorindexoutofboundsexception

Android CursorIndexOutOfBoundsException Index 0 requested, with a size of 0


I am trying to get 2 different ListView's in my Activity to be populated with database content onCreate. Every since I added the code, I'm now getting a force close with the error mentioned in the title.

I have no idea why this is happening.

Heres the code:

    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);

Here is the structure and functions for the database:

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"
            + ")");

Can someone shed some light on why this is happening? If I have missed any important code please let me know and I will update the question.


Solution

  • Try to use different cursor instance for both list.

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

    and

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