Edward Sullen
Edward Sullen

Reputation: 157

Cursor - Android

Is it possible to use 2 different cursors for selecting table :

this for a table1

Cursor b = myDbHelper.getSchoolType();
String def = b.getString(0);

and this for another table

c = myDbHelper.getAllSchoolData();
String abc = c.getString(0);

I try to retrieve data from two table useing two cursor but it show error :

android.database.CursorIndexOutOfBoundsException : Index -1 requested, with a size of 2

please help... thanks for all kinds of answer

Upvotes: 0

Views: 360

Answers (2)

m0skit0
m0skit0

Reputation: 25873

Cursor index is placed at -1 when retreived. You need to move it to first result. For this you have different choices, but this is how I use it:

Cursor c = // Retreive your cursor;
if (c != null) {
    while(c.moveToNext()) {
        // Code for each row
    }
}

This will loop over all cursor rows.

You should always read Android tutorials or examine the SDK reference. Learning to read documentation is a basic skill for a programmer.

Upvotes: 0

Ziteng Chen
Ziteng Chen

Reputation: 1969

By default the cursor pointer points to the position before the first entry (row), so you need to move it to a proper position before you can read data from it. For example move to the first entry:

if(cursor != null && cursor.moveToFirst()) 
    String value = cursor.getString(0);
    ...
}

There are similar methods like moveToNext(), moveToLast() etc, see the reference page for more.

Upvotes: 1

Related Questions