1

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

这是单击确定时发生的情况:

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)
4

2 回答 2

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});
于 2012-06-24T13:30:52.687 回答
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()

于 2012-06-24T13:53:36.177 回答