这应该有用吗?
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)
答案 0 :(得分:2)
一旦内部循环完成,光标就在它的末尾,因此当外部循环导致内部循环再次运行时,它正在尝试移动到下一个项目(不存在)。您需要告诉第一个Cursor重置为其初始位置。
c1.moveToFirst();
在内环正上方的外环内。
答案 1 :(得分:1)
你有' IndexOutOfBoundsException'
当你完成对c1的迭代,c1指向一个不存在的元素时,会发生什么
来自: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.
所以在内部do while循环结束后,你的c1 Cursor处于无效状态,你需要将它重置为开头。
答案 2 :(得分:1)
一旦内循环结束,c1将越过最后位置。当内循环再次启动时,您试图访问超出c1结束位置的值。因此,它抛出IndexOutOfBoundException ...
如果你想在开始下一个内循环之前再次使用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());