这应该有效吗?
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());
如果我删除其中一个 do while 循环,它就可以正常工作,每个单独的循环都会毫无问题地遍历每个光标条目。
但是在两个循环都完好无损的情况下,我不断收到以下错误:
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)
最佳答案
一旦内部循环完成,光标就位于其末尾,因此当外部循环导致内部循环再次运行时,它会尝试移动到下一个项目(该项目不存在)。您需要告诉第一个光标重置到其初始位置。
c1.moveToFirst();
在内循环正上方的外循环内部。
关于java - 嵌套的 Do-While 循环通过游标进行索引 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22513568/