简体   繁体   中英

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)

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.

You have and 'IndexOutOfBoundsException'

What is happening is when you finish iterating over c1, c1 is pointing to a nonexisting element

from: Documentation: moveToNext ()

Move the cursor to the next row.

This method will return false if the cursor is already past the last entry in the result set.

Returns
whether the move succeeded.

So after the inner do while loop finishes, your c1 Cursor is in an invalid state, and you need to reset it to the beginning.

Once the inner loop finishes, c1 will cross last position. when inner loop start again you are trying to access value beyond the end position of c1. Because of this, it is throwing IndexOutOfBoundException...

if you want use c1 again call c1.moveToFirst() before starting next inner loop

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

do{

    c1.moveToFirst();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM