Search code examples
javaandroidwhile-loopcursordo-while

Nested Do-While loops both indexing through cursors - not working


Should this work?

Cursor c1 = db.m1();
Cursor c2 = db.m2();

do{

    do{
        String foodDate = c1.getString(1);
        String foodTime = c1.getString(2);

        String sympDate = c2.getString(1);
        String sympTime = c2.getString(2);
        // more stuff
    }while(c1.moveToNext());

}while(c2.moveToNext());    

It works fine if I remove either of the do while loops, each individual loop will go through each cursor entry no problem.

But with both loops intact I keep getting the following errors:

03-19 13:09:05.751: W/dalvikvm(3616): threadid=1: thread exiting with uncaught exception (group=0xb3a54b90)
03-19 13:09:05.761: E/AndroidRuntime(3616): FATAL EXCEPTION: main
03-19 13:09:05.761: E/AndroidRuntime(3616): java.lang.IllegalStateException: Could not execute method of the activity
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.view.View$1.onClick(View.java:3814)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.view.View.performClick(View.java:4424)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.view.View$PerformClick.run(View.java:18383)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.os.Handler.handleCallback(Handler.java:733)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.os.Looper.loop(Looper.java:137)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at java.lang.reflect.Method.invoke(Method.java:515)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at dalvik.system.NativeStart.main(Native Method)
03-19 13:09:05.761: E/AndroidRuntime(3616): Caused by: java.lang.reflect.InvocationTargetException
03-19 13:09:05.761: E/AndroidRuntime(3616):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at java.lang.reflect.Method.invoke(Method.java:515)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.view.View$1.onClick(View.java:3809)
03-19 13:09:05.761: E/AndroidRuntime(3616):     ... 11 more
03-19 13:09:05.761: E/AndroidRuntime(3616): Caused by: android.database.CursorIndexOutOfBoundsException: Index 210 requested, with a size of 210
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
03-19 13:09:05.761: E/AndroidRuntime(3616):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)

Solution

  • Once the inner loop completes, the cursor is at its end, therefore when the outer loop causes the inner loop to run again, it's attempting to move to the next item (which doesnt exist). You'll need to tell the first Cursor to reset to its initial position.

    c1.moveToFirst();
    

    inside the outer loop right above the inner loop.