我有一个程序,你点击一个按钮,弹出一个带有一堆复选框的对话框。您可以选择一些复选框,然后单击“确定”。我想要做的就是让你按下ok,对于你检查过的每个值,它会在几个数据库表中搜索包含该值的行,从这些行获取信息,并将它们放在不同的表中。由于某种原因,我一直把这个光标超出界限异常,老实说我不明白为什么。我尝试了一些我在搜索时发现的东西,但是我无法让它工作。
点击确定后会发生以下情况:
ok.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (dumbbells.isChecked()==true){
equip = "dumbbells";
AddToEquipTable(equip);
}
if (barbell.isChecked()==true){
equip = "Barbell";
AddToEquipTable(equip);
}
if (cablemachine.isChecked()==true){
equip = "Cable Machine";
AddToEquipTable(equip);
}
if (pullupbar.isChecked()==true){
equip = "Pull Up Bar";
AddToEquipTable(equip);
}
if (legpressmachine.isChecked()==true){
equip = "Leg Press Machine";
AddToEquipTable(equip);
}
if (legextensionmachine.isChecked()==true){
equip = "Leg Extension Machine";
AddToEquipTable(equip);
}
if (hamstringcurlmachine.isChecked()==true){
equip = "Hamstring Curl Machine";
AddToEquipTable(equip);
}
if (kettlebells.isChecked()==true){
equip = "Kettlebells";
AddToEquipTable(equip);
}
if (none.isChecked()==true){
equip = "None";
AddToEquipTable(equip);
}
}
});
这是我创建的“AddToEquipTable”方法:
public void AddToEquipTable(String equip){
ExerciseDatabase equipment = new ExerciseDatabase(WorkMeOutActivity.this);
try {
equipment.open();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
equipment.AddToEquipmentDBFromBiceps(equip);
equipment.AddToEquipmentDBFromChest(equip);
equipment.AddToEquipmentDBFromBack(equip);
equipment.AddToEquipmentDBFromTriceps(equip);
equipment.AddToEquipmentDBFromShoulders(equip);
equipment.AddToEquipmentDBFromLegs(equip);
equipment.AddToEquipmentDBFromCompound(equip);
equipment.close();
}
这是我在DB Helper Activity中调用的方法之一。它们都是相同的,除了名为。
的表名public String AddToEquipmentDBFromBiceps(String equip) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_REPS, KEY_EXERCISE, KEY_TIMETYPE};
ContentValues cv = new ContentValues();
Cursor c = ourDatabase.query(DATABASE_BICEPSTABLE, columns, KEY_EQUIP + "='" + equip + "'", null, null, null, null);
if (c == null){
return null;
}
else if(c != null){
c.moveToFirst();
int iReps = c.getColumnIndex(KEY_REPS);
int iExercise = c.getColumnIndex(KEY_EXERCISE);
int iTimeType = c.getColumnIndex(KEY_TIMETYPE);
do{
String reps = c.getString(iReps);
String exercise = c.getString(iExercise);
String time = c.getString(iTimeType);
cv.put(KEY_REPS, reps);
cv.put(KEY_EXERCISE, exercise);
cv.put(KEY_TIMETYPE, time);
ourDatabase.insert(DATABASE_CHOSEN_EQUIP_TABLE, null, cv);
c.moveToNext();
}while(KEY_EQUIP == equip);
}
return null;
}
**更改条件后的错误日志建议如下:
06-24 09:34:42.785: E/AndroidRuntime(20592): FATAL EXCEPTION: main
06-24 09:34:42.785: E/AndroidRuntime(20592): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
06-24 09:34:42.785: E/AndroidRuntime(20592): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
06-24 09:34:42.785: E/AndroidRuntime(20592): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
06-24 09:34:42.785: E/AndroidRuntime(20592): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
06-24 09:34:42.785: E/AndroidRuntime(20592): at com.work.me.out.ExerciseDatabase.AddToEquipmentDBFromLegs(ExerciseDatabase.java:896)
06-24 09:34:42.785: E/AndroidRuntime(20592): at com.work.me.out.WorkMeOutActivity.AddToEquipTable(WorkMeOutActivity.java:456)
06-24 09:34:42.785: E/AndroidRuntime(20592): at com.work.me.out.WorkMeOutActivity$9.onClick(WorkMeOutActivity.java:395)
06-24 09:34:42.785: E/AndroidRuntime(20592): at android.view.View.performClick(View.java:2538)
06-24 09:34:42.785: E/AndroidRuntime(20592): at android.view.View$PerformClick.run(View.java:9152)
06-24 09:34:42.785: E/AndroidRuntime(20592): at android.os.Handler.handleCallback(Handler.java:587)
06-24 09:34:42.785: E/AndroidRuntime(20592): at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 09:34:42.785: E/AndroidRuntime(20592): at android.os.Looper.loop(Looper.java:130)
06-24 09:34:42.785: E/AndroidRuntime(20592): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-24 09:34:42.785: E/AndroidRuntime(20592): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 09:34:42.785: E/AndroidRuntime(20592): at java.lang.reflect.Method.invoke(Method.java:507)
06-24 09:34:42.785: E/AndroidRuntime(20592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
06-24 09:34:42.785: E/AndroidRuntime(20592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-24 09:34:42.785: E/AndroidRuntime(20592): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
可能你情况不好,改变你的情况:
while (c.moveToNext() && KEY_EQUIP == equip);
注意:我还建议您使用更安全,更快速,更清晰的parametrized statements
。
CursorIndexOutOfBoundsException:请求索引0,大小为0
这意味着cursor.moveToFirst()
返回false而Cursor
为空。
因此,请尝试添加此代码段并尝试使用。
else if(c != null && c.moveToFirst()) {
int iReps = c.getColumnIndex(KEY_REPS);
int iExercise = c.getColumnIndex(KEY_EXERCISE);
int iTimeType = c.getColumnIndex(KEY_TIMETYPE);
do {
String reps = c.getString(iReps);
String exercise = c.getString(iExercise);
String time = c.getString(iTimeType);
cv.put(KEY_REPS, reps);
cv.put(KEY_EXERCISE, exercise);
cv.put(KEY_TIMETYPE, time);
ourDatabase.insert(DATABASE_CHOSEN_EQUIP_TABLE, null, cv);
} while (c.moveToNext() && KEY_EQUIP == equip);
}
尝试使用类似这样的rawQuery
方法:
final String SELECT_QUERY = "SELECT " + KEY_REPS + ", " + KEY_EXERCISE + ", " + KEY_TIMETYPE + " FROM " + DATABASE_BICEPSTABLE + " WHERE " + KEY_EQUIP + " = ?";
Cursor c = db.rawQuery(SELECT_QUERY, new String[] {equip});
答案 1 :(得分:1)
在尝试访问任何数据(尤其是行c.getColumnIndex(KEY_XXX)
)之前,您必须确保指向光标的第一行。您已调用c.moveToFirst()
而未检查其结果(在您的情况下应该返回false,因为没有数据行。
if(c != null && c.moveToFirst()) {
int iReps = c.getColumnIndex(KEY_REPS);
int iExercise = c.getColumnIndex(KEY_EXERCISE);
int iTimeType = c.getColumnIndex(KEY_TIMETYPE);
do {
String reps = c.getString(iReps);
String exercise = c.getString(iExercise);
String time = c.getString(iTimeType);
cv.put(KEY_REPS, reps);
cv.put(KEY_EXERCISE, exercise);
cv.put(KEY_TIMETYPE, time);
ourDatabase.insert(DATABASE_CHOSEN_EQUIP_TABLE, null, cv);
} while(c.moveToNext() && KEY_EQUIP == equip);
} else {
return null;
}
请注意,在此处使用do..while
循环而不是常规while
循环非常重要,因为调用c.moveToFirst()
后紧跟c.moveToNext()
意味着您错过了第一行游标数据!