/** * Gets the exercises from our database that are associated with the given workout id * * @param workoutId - the id of the workout we want the exercises for * @return - List of exercises associated with given workout id. */ public ArrayList<Exercise> getExercises(long workoutId) { // Create a list of exercises that we'll be adding to ArrayList<Exercise> prevExercises = new ArrayList<Exercise>(); // Query for exercises associated with that workout id. Cursor exerciseCursor = database.rawQuery( "SELECT * FROM exercise WHERE workout_id = ? ORDER BY _id ASC", new String[] {"" + workoutId}); // For every exercise associated with this workout... while (exerciseCursor.moveToNext()) { // Grab the exercise id long exerciseId = exerciseCursor.getLong(exerciseCursor.getColumnIndex(WorkoutDBSQLiteHelper.EXERCISE_ID)); // Grab the exercise name and intensity and create our exercise object as well as the previous // reps list String exerciseName = exerciseCursor.getString( exerciseCursor.getColumnIndex(WorkoutDBSQLiteHelper.EXERCISE_NAME)); int exerciseWeight = exerciseCursor.getInt( exerciseCursor.getColumnIndex(WorkoutDBSQLiteHelper.EXERCISE_INTENSITY)); Exercise exer = new Exercise(exerciseName, exerciseWeight); // Set the last reps for our exercise and add it to the exercise list. exer.setLastReps(getReps(exerciseId)); prevExercises.add(exer); } return prevExercises; }
public Workout insertWorkout(Workout workout) { long workoutId, exerciseId, repId; // Pack our values for the workout table ContentValues values = new ContentValues(); values.put(WorkoutDBSQLiteHelper.WORKOUT_NAME, workout.getName()); values.put(WorkoutDBSQLiteHelper.WORKOUT_FAVORITE, workout.getFavorite()); values.put(WorkoutDBSQLiteHelper.WORKOUT_DATE, getCurrentDate()); // Attempt to insert the values into the database workoutId = database.insert(WorkoutDBSQLiteHelper.TABLE_WORKOUT, null, values); // If the insert failed, return null if (workoutId == -1) { return null; } // For each of the completed exercises... for (Exercise exer : workout.getCompleteExercises()) { // Pack our values for the exercise table. values = new ContentValues(); values.put(WorkoutDBSQLiteHelper.EXERCISE_NAME, exer.getName()); values.put(WorkoutDBSQLiteHelper.EXERCISE_WORKOUT_ID, workoutId); values.put(WorkoutDBSQLiteHelper.EXERCISE_INTENSITY, exer.getNewWeight()); // Attempt to insert the values into the database exerciseId = database.insert(WorkoutDBSQLiteHelper.TABLE_EXERCISE, null, values); // If the insert failed, return null if (exerciseId == -1) { return null; } for (Integer integer : exer.getReps()) { // Pack our values for the reps table values = new ContentValues(); values.put(WorkoutDBSQLiteHelper.REP_EXERCISE_ID, exerciseId); values.put(WorkoutDBSQLiteHelper.REP_COUNT, integer.intValue()); // Attempt to insert the values into the database. repId = database.insert(WorkoutDBSQLiteHelper.TABLE_REP, null, values); // If the insert failed, return null if (repId == -1) { return null; } } } return workout; }