I have a program where you click a button, and a dialog pops up with a bunch of checkboxes. You choose a few of the checkboxes, and then click “ok”. What I am trying to do is have it so that when you press ok, for each value you checked off, it will search several database tables for rows containing that value, take information from those rows, and put them in a different table. For some reason I keep getting this cursor out of bounds exception and I honestly cannot figure out why. I tried a bunch of things that I found while searching and I cannot get it to work.
Here is what happens when you click 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);
}
}
});
Here is the “AddToEquipTable” method I created:
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();
}
And here is one of the methods it calls in my DB Helper Activity. They are all the same, except for the table name called.
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;
}
**Error Log After Changing Condition as Suggested Below:
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)
Probably you have bad condition, change yours with:
Note: Also i recommend to you use
parametrized statementswhich are more safe, faster and clearer.It means that
cursor.moveToFirst()returns false andCursoris empty.So try to add this snippet and try it.
Try to use
rawQuerymethod similar like this: