tags:

views:

22

answers:

1

I'm attempting to loop through database values creating tabs. I have setup a method called createTab which accepts a Long value and a String value. It's working with static data, but I'm struggling to understand how to loop through SQLite database records.

Here is my failed attempt (replace [lessthan] with less than symbol):


for (int i = 0; i [lessthan] mCursor.getCount(); i++)
{
 createTab(
  mCursor.getLong(mCursor.getColumnIndexOrThrow("_id")),
  mCursor.getString(mCursor.getColumnIndexOrThrow("category")));
}

You probably don't need the LogCat to know what I did wrong in the code above, but just in case...

08-27 21:28:18.268: ERROR/AndroidRuntime(232): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:99)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at com.toBuy.Main.createTabs(Main.java:51)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at com.toBuy.Main.onCreate(Main.java:38)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
08-27 21:28:18.268: ERROR/AndroidRuntime(232):     ... 11 more
08-27 21:28:18.278: INFO/Process(54): Sending signal. PID: 232 SIG: 3
08-27 21:28:18.278: INFO/dalvikvm(232): threadid=7: reacting to signal 3
08-27 21:28:18.338: INFO/dalvikvm(232): Wrote stack trace to '/data/anr/traces.txt'
08-27 21:28:18.508: INFO/ARMAssembler(54): generated scanline__00000077:03515104_00000000_00000000 [ 27 ipp] (41 ins) at [0x285a68:0x285b0c] in 713498 ns
08-27 21:28:18.518: INFO/ARMAssembler(54): generated scanline__00000077:03515104_00001001_00000000 [ 64 ipp] (84 ins) at [0x285b10:0x285c60] in 1897448 ns
08-27 21:28:28.086: WARN/ActivityManager(54): Launch timeout has expired, giving up wake lock!
08-27 21:28:28.097: WARN/ActivityManager(54): Activity idle timeout for HistoryRecord{4390bf70 com.toBuy/.Main}

Thank you for your help.

A: 

You need to position the cursor before you can call getLong or getString. After you issue a query, the Cursor is positioned just before the first row, so you should call moveToNext in your loop. Something like:

int size = mCursor.getCount();
for (int i = 0; i < size; i++) {

    // Position the cursor
    mCursor.moveToNext();

    // Fetch your data values
    long id = mCursor.getLong(mCursor.getColumnIndex("_id"));
    String cat = mCursor.getString(mCursor.getColumnIndex("category"));
}
Erich Douglass
Thank you for the response. What I'm struggling with at this point is referencing the columns within the cursor.How would the // Fetch your data values look for a Long columns (_id) and a String columns (category)?
alockrem
You'd still access the column values exactly like you're doing in your sample. The Cursor object contains a pointer to the row in the database that it's currently pointing at, and your sample code just needed to position it before calling any get* methods. If that doesn't fix your problem, I'd pull the database off of the device (or emulator) and double check the schema.
Erich Douglass