I am making a simple workout calender android app. I want to query my database for certain dates and display those rows. I am just learning how to use databases by looking at tutorials but I seem to be stuck at the moment. Any help would be appreciated. Here is my code for the query I tried.
Workout getWorkout(String date){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_WORKOUT, new String[] {KEY_DATE, KEY_WORKOUT, KEY_REPS, KEY_WEIGHT, KEY_ONE_REP_MAX} ,
KEY_DATE+ "=?", new String[] { date }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Workout workout = new Workout(cursor.getString(1), cursor.getString(2),
Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)));
return workout;
}
Here is where I try to display the results. The app stops working when I have Workout workouts = db.getWorkout(date);
in the code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_notes);
// Show the Up button in the action bar.
setupActionBar();
Intent intent = getIntent();
String Mdate = intent.getStringExtra(MainActivity.MONTH_DATE);
String Ydate = intent.getStringExtra(MainActivity.YEAR_DATE);
String Ddate = intent.getStringExtra(MainActivity.DAY_DATE);
final TextView textViewToChange = (TextView) findViewById(R.id.textView1);
Month = Mdate;
Year = Ydate;
Day = Ddate;
String date = (Month+"/" +Day+"/" +Year);
setTitle(date);
Databasehandler db = new Databasehandler(this);
Workout workouts = db.getWorkout(date);
String dataInfo = (workouts.getWorkout() + workouts.getReps() + workouts.getWeight());
textViewToChange.setText(dataInfo);
}
LogCat:
11-18 23:30:28.825: D/AndroidRuntime(2137): Shutting down VM 11-18 23:30:28.825: W/dalvikvm(2137): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 11-18 23:30:28.866: E/AndroidRuntime(2137): FATAL EXCEPTION: main 11-18 23:30:28.866: E/AndroidRuntime(2137): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workoutcalender/com.example.workoutcalender.WorkoutNotes}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.app.ActivityThread.access$600(ActivityThread.java:141) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 11-18 23:30:28.866: E/Androi dRuntime(2137): at android.os.Handler.dispatchMessage(Handler.java:99) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.os.Looper.loop(Looper.java:137) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.app.ActivityThread.main(ActivityThread.java:5041) 11-18 23:30:28.866: E/AndroidRuntime(2137): at java.lang.reflect.Method.invokeNative(Native Method) 11-18 23:30:28.866: E/AndroidRuntime(2137): at java.lang.reflect.Method.invoke(Method.java:511) 11-18 23:30:28.866: E/AndroidRuntime(2137): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 11-18 23:30:28.866: E/AndroidRuntime(2137): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 11-18 23:30:28.866: E/AndroidRuntime(2137): at dalvik.system.NativeStart.main(Native Method) 11-18 23:30:28.866: E/AndroidRuntime(2137): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.database.Abstract Cursor.checkPosition(AbstractCursor.java:424) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 11-18 23:30:28.866: E/AndroidRuntime(2137): at com.example.workoutcalender.Databasehandler.getWorkout(Databasehandler.java:70) 11-18 23:30:28.866: E/AndroidRuntime(2137): at com.example.workoutcalender.WorkoutNotes.onCreate(WorkoutNotes.java:48) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.app.Activity.performCreate(Activity.java:5104) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 11-18 23:30:28.866: E/AndroidRuntime(2137): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 11-18 23:30:28.866: E/AndroidRuntime(2137): ... 11 more
I am learning android programming on my own through tutorials so try not to be too harsh if this sucks. If you need more information let me know please.
In this code:
Cursor cursor = db.query(...);
if (cursor != null)
cursor.moveToFirst();
you do not need to check cursor
for null
, because query
always return a cursor object.
However, you must check the return value of moveToFirst
because the cursor might not contain any data; this case must be handled in some way:
Cursor cursor = db.query(...);
if (cursor.moveToFirst()) {
...
return workout;
} else {
return null; // or throw an exception, or handle it in some other way
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.