コード例 #1
0
  public Exercise getExerciseDetail(int id) {
    SQLiteDatabase db = this.getWritableDatabase();

    String selectQuery = "SELECT * FROM " + TABLE_EXERCISE_DETAIL + " WHERE " + KEY_ID + " = " + id;
    Cursor c = db.rawQuery(selectQuery, null);

    if (c != null) c.moveToFirst();
    Exercise exercise = new Exercise();
    exercise.setId(c.getInt(c.getColumnIndex(KEY_ID)));
    exercise.setName(c.getString(c.getColumnIndex(KEY_NAME)));
    exercise.setDescription(c.getString(c.getColumnIndex(KEY_DESCRIPTION)));

    selectQuery =
        "SELECT * FROM "
            + TABLE_EXERCISE_DETAIL_CATEGORY
            + " WHERE "
            + TABLE_EXERCISE_DETAIL
            + KEY_ID
            + " = "
            + exercise.getId();
    c.close();
    c = db.rawQuery(selectQuery, null);
    while (c.moveToNext()) {
      exercise.addCategory(
          Category.valueOf(c.getString(c.getColumnIndex(TABLE_EXERCISE_CATEGORY + KEY_NAME))));
    }
    c.close();
    return exercise;
  }
コード例 #2
0
  public List<Exercise> getAllExerciseDetails() {
    List<Exercise> exercises = new ArrayList<>();
    String selectQuery =
        "SELECT "
            + KEY_ID
            + ", "
            + KEY_NAME
            + ", "
            + KEY_DESCRIPTION
            + ", "
            + TABLE_EXERCISE_CATEGORY
            + KEY_NAME
            + " FROM "
            + TABLE_EXERCISE_DETAIL
            + " INNER JOIN "
            + TABLE_EXERCISE_DETAIL_CATEGORY
            + " ON "
            + KEY_ID
            + " = "
            + TABLE_EXERCISE_DETAIL
            + KEY_ID;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    int id;
    Exercise exercise;
    if (c.moveToFirst()) {
      do {
        id = c.getInt(c.getColumnIndex(KEY_ID));
        exercise = Exercise.getById(exercises, id);
        if (exercise == null) {
          exercise = new Exercise();
          exercise.setId(id);
          exercise.setName(c.getString(c.getColumnIndex(KEY_NAME)));
          exercise.setDescription(c.getString(c.getColumnIndex(KEY_DESCRIPTION)));
          exercise
              .getCategories()
              .add(
                  Category.valueOf(
                      c.getString(c.getColumnIndex(TABLE_EXERCISE_CATEGORY + KEY_NAME))));

          exercises.add(exercise);
        } else {
          exercise
              .getCategories()
              .add(
                  Category.valueOf(
                      c.getString(c.getColumnIndex(TABLE_EXERCISE_CATEGORY + KEY_NAME))));
        }
      } while (c.moveToNext());
    }
    c.close();
    return exercises;
  }