简体   繁体   English

嵌套Do-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. 如果删除do while循环中的任何一个,它都可以正常工作,每个单独的循环都会遍历每个游标条目,没有问题。

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. 您需要告诉第一个Cursor重置为其初始位置。

c1.moveToFirst();

inside the outer loop right above the inner loop. 在内部循环上方的外部循环中。

You have and 'IndexOutOfBoundsException' 你有'IndexOutOfBoundsException'

What is happening is when you finish iterating over c1, c1 is pointing to a nonexisting element 发生的情况是,当您完成对c1的迭代时,c1指向不存在的元素

from: Documentation: moveToNext () 来自: 文档: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. 因此,在内部do while循环完成之后,您的c1游标处于无效状态,因此您需要将其重置为开始。

Once the inner loop finishes, c1 will cross last position. 内循环完成后,c1将越过最后一个位置。 when inner loop start again you are trying to access value beyond the end position of c1. 当内循环再次开始时,您尝试访问的值超出c1的结束位置。 Because of this, it is throwing IndexOutOfBoundException... 因此,它抛出IndexOutOfBoundException ...

if you want use c1 again call c1.moveToFirst() before starting next inner loop 如果要使用c1,请在开始下一个内部循环之前再次调用c1.moveToFirst()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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