我有一个程序,您可以在其中单击一个按钮,然后会弹出一个带有一堆复选框的对话框。您选择几个复选框,然后单击“确定”。我想要做的是拥有它,这样当您按下确定时,对于您检查的每个值,它将在多个数据库表中搜索包含该值的行,从这些行中获取信息,并将它们放在不同的表中。出于某种原因,我一直让这个光标超出边界异常,老实说,我不知道为什么。我尝试了一堆我在搜索时发现的东西,但我无法让它工作。
这是单击确定时发生的情况:
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)