Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DatabaseHelper extends SQLiteOpenHelper {
- public static String DATABASE_NAME = "workout_database";
- private static final int DATABASE_VERSION = 1;
- //workout table
- private static final String TABLE_WORKOUT = "workout_table";
- private static final String COL1 = "_id";
- private static final String COL2 = "workout_name";
- //exercise table
- private static final String TABLE_EXERCISE = "exercise_table";
- private static final String COL3 = "id";
- private static final String COL4 = "exercise_name";
- private static final String COL5 = "exercise_set";
- private static final String COL6 = "exercise_weight";
- private static final String COL7 = "exercise_reps";
- private static final String createWorkoutTable = "CREATE TABLE " + TABLE_WORKOUT + "("
- + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
- + COL2 + " TEXT NOT NULL );";
- private static final String createExerciseTable = "CREATE TABLE " + TABLE_EXERCISE + "("
- + COL3 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
- + COL4 + " TEXT NOT NULL, "
- + COL5 + " INTEGER NOT NULL, "
- + COL6 + " INTEGER NOT NULL, "
- + COL7 + " INTEGER NOT NULL );";
- public DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- Log.d("table", createWorkoutTable);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(createWorkoutTable);
- db.execSQL(createExerciseTable);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS '" + TABLE_WORKOUT + " ' ");
- db.execSQL("DROP TABLE IF EXISTS '" + TABLE_EXERCISE + " ' ");
- onCreate(db);
- }
- public void addWorkout(String name){
- SQLiteDatabase db = this.getWritableDatabase();
- //adding workout name in workout table
- ContentValues values = new ContentValues();
- values.put(COL2, name);
- db.insert(TABLE_WORKOUT , null, values);
- }
- public void addExercise(String name, String set, String weight, String reps){
- SQLiteDatabase db = this.getWritableDatabase();
- //adding exercise in exercise table
- ContentValues cv = new ContentValues();
- cv.put(COL4, name);
- cv.put(COL5, set);
- cv.put(COL6, weight);
- cv.put(COL7, reps);
- db.insert(TABLE_EXERCISE, null, cv);
- }
- public ArrayList<Workout> getAllWokouts(){
- ArrayList<Workout> workoutArrayList = new ArrayList<Workout>();
- SQLiteDatabase db = this.getReadableDatabase();
- try{
- String selectQuery = "SELECT * FROM " + TABLE_WORKOUT;
- db = this.getReadableDatabase();
- Cursor c = db.rawQuery(selectQuery, null);
- if (c != null && c.moveToFirst()){
- do {
- Workout workout = new Workout();
- workout.setId(c.getInt(0));
- workout.setName(c.getString(1));
- workoutArrayList.add(workout);
- } while (c.moveToNext());
- }
- return workoutArrayList;
- } catch (SQLiteException se){
- Log.v("Exception",
- Log.getStackTraceString(se));
- } catch (Exception e){
- Log.v("Exception",
- Log.getStackTraceString(e));
- } finally {
- db.close();
- }
- return workoutArrayList;
- }
- public ArrayList<Exercise> getAllExercise(){
- ArrayList<Exercise> exerciseArrayList = new ArrayList<Exercise>();
- SQLiteDatabase db = this.getReadableDatabase();
- try{
- String selectQuery = "SELECT * FROM " + TABLE_EXERCISE;
- Cursor cursor = db.rawQuery(selectQuery, null);
- if (cursor != null && cursor.moveToFirst()){
- do {
- Exercise exercise = new Exercise();
- exercise.setId(cursor.getInt(0));
- exercise.setName(cursor.getString(1));
- exercise.setSet(cursor.getInt(2));
- exercise.setWeight(cursor.getInt(3));
- exercise.setReps(cursor.getInt(4));
- exerciseArrayList.add(exercise);
- } while (cursor.moveToNext());
- }
- return exerciseArrayList;
- } catch (SQLiteException se){
- Log.v("Exception",
- Log.getStackTraceString(se));
- } catch (Exception e){
- Log.v("Exception",
- Log.getStackTraceString(e));
- } finally {
- db.close();
- }
- return exerciseArrayList;
- }
- }
- private ListView listView;
- private ArrayList<Workout> workoutArrayList;
- private WorkoutAdapter workoutAdapter;
- private DatabaseHelper databaseHelper;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activty_get_all_workouts);
- listView = (ListView) findViewById(R.id.lv);
- databaseHelper = new DatabaseHelper(this);
- workoutArrayList = databaseHelper.getAllWokouts();
- workoutAdapter = new WorkoutAdapter(this,workoutArrayList);
- listView.setAdapter(workoutAdapter);
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Intent intent = new Intent(GetAllWorkoutsActivity.this,AddExercises.class);
- intent.putExtra("workout_name", workoutArrayList.get(position));
- startActivity(intent);
- }
- });
- }
- private ListView listView;
- private ArrayList<Exercise> exerciseArrayList;
- private ExerciseAdapter exerciseAdapter;
- private DatabaseHelper databaseHelper;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_get_all_exercises);
- listView = (ListView) findViewById(R.id.lv2);
- databaseHelper = new DatabaseHelper(this);
- exerciseArrayList = databaseHelper.getAllExercise();
- exerciseAdapter = new ExerciseAdapter(this,exerciseArrayList);
- listView.setAdapter(exerciseAdapter);
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Intent intent = new Intent(GetAllExercisesActivity.this,AddExercises.class);
- intent.putExtra("user", exerciseArrayList.get(position));
- startActivity(intent);
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment