Guest User

Untitled

a guest
Apr 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2.  
  3. public static String DATABASE_NAME = "workout_database";
  4. private static final int DATABASE_VERSION = 1;
  5.  
  6. //workout table
  7. private static final String TABLE_WORKOUT = "workout_table";
  8. private static final String COL1 = "_id";
  9. private static final String COL2 = "workout_name";
  10.  
  11. //exercise table
  12. private static final String TABLE_EXERCISE = "exercise_table";
  13. private static final String COL3 = "id";
  14. private static final String COL4 = "exercise_name";
  15. private static final String COL5 = "exercise_set";
  16. private static final String COL6 = "exercise_weight";
  17. private static final String COL7 = "exercise_reps";
  18.  
  19. private static final String createWorkoutTable = "CREATE TABLE " + TABLE_WORKOUT + "("
  20. + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
  21. + COL2 + " TEXT NOT NULL );";
  22.  
  23.  
  24. private static final String createExerciseTable = "CREATE TABLE " + TABLE_EXERCISE + "("
  25. + COL3 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
  26. + COL4 + " TEXT NOT NULL, "
  27. + COL5 + " INTEGER NOT NULL, "
  28. + COL6 + " INTEGER NOT NULL, "
  29. + COL7 + " INTEGER NOT NULL );";
  30.  
  31.  
  32.  
  33. public DatabaseHelper(Context context) {
  34. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  35.  
  36. Log.d("table", createWorkoutTable);
  37. }
  38.  
  39. @Override
  40. public void onCreate(SQLiteDatabase db) {
  41. db.execSQL(createWorkoutTable);
  42. db.execSQL(createExerciseTable);
  43.  
  44. }
  45.  
  46. @Override
  47. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  48. db.execSQL("DROP TABLE IF EXISTS '" + TABLE_WORKOUT + " ' ");
  49. db.execSQL("DROP TABLE IF EXISTS '" + TABLE_EXERCISE + " ' ");
  50. onCreate(db);
  51.  
  52. }
  53.  
  54. public void addWorkout(String name){
  55. SQLiteDatabase db = this.getWritableDatabase();
  56.  
  57.  
  58.  
  59. //adding workout name in workout table
  60. ContentValues values = new ContentValues();
  61. values.put(COL2, name);
  62. db.insert(TABLE_WORKOUT , null, values);
  63. }
  64.  
  65. public void addExercise(String name, String set, String weight, String reps){
  66.  
  67. SQLiteDatabase db = this.getWritableDatabase();
  68.  
  69. //adding exercise in exercise table
  70. ContentValues cv = new ContentValues();
  71. cv.put(COL4, name);
  72. cv.put(COL5, set);
  73. cv.put(COL6, weight);
  74. cv.put(COL7, reps);
  75. db.insert(TABLE_EXERCISE, null, cv);
  76.  
  77. }
  78.  
  79. public ArrayList<Workout> getAllWokouts(){
  80. ArrayList<Workout> workoutArrayList = new ArrayList<Workout>();
  81. SQLiteDatabase db = this.getReadableDatabase();
  82.  
  83. try{
  84. String selectQuery = "SELECT * FROM " + TABLE_WORKOUT;
  85. db = this.getReadableDatabase();
  86. Cursor c = db.rawQuery(selectQuery, null);
  87.  
  88. if (c != null && c.moveToFirst()){
  89. do {
  90. Workout workout = new Workout();
  91. workout.setId(c.getInt(0));
  92. workout.setName(c.getString(1));
  93.  
  94. workoutArrayList.add(workout);
  95.  
  96.  
  97. } while (c.moveToNext());
  98. }
  99.  
  100. return workoutArrayList;
  101. } catch (SQLiteException se){
  102. Log.v("Exception",
  103. Log.getStackTraceString(se));
  104.  
  105. } catch (Exception e){
  106. Log.v("Exception",
  107. Log.getStackTraceString(e));
  108. } finally {
  109. db.close();
  110. }
  111. return workoutArrayList;
  112. }
  113.  
  114.  
  115.  
  116. public ArrayList<Exercise> getAllExercise(){
  117. ArrayList<Exercise> exerciseArrayList = new ArrayList<Exercise>();
  118. SQLiteDatabase db = this.getReadableDatabase();
  119.  
  120. try{
  121. String selectQuery = "SELECT * FROM " + TABLE_EXERCISE;
  122. Cursor cursor = db.rawQuery(selectQuery, null);
  123. if (cursor != null && cursor.moveToFirst()){
  124. do {
  125. Exercise exercise = new Exercise();
  126. exercise.setId(cursor.getInt(0));
  127. exercise.setName(cursor.getString(1));
  128. exercise.setSet(cursor.getInt(2));
  129. exercise.setWeight(cursor.getInt(3));
  130. exercise.setReps(cursor.getInt(4));
  131.  
  132. exerciseArrayList.add(exercise);
  133. } while (cursor.moveToNext());
  134. }
  135. return exerciseArrayList;
  136. } catch (SQLiteException se){
  137. Log.v("Exception",
  138. Log.getStackTraceString(se));
  139.  
  140. } catch (Exception e){
  141. Log.v("Exception",
  142. Log.getStackTraceString(e));
  143. } finally {
  144. db.close();
  145. }
  146. return exerciseArrayList;
  147. }
  148.  
  149. }
  150.  
  151. private ListView listView;
  152. private ArrayList<Workout> workoutArrayList;
  153. private WorkoutAdapter workoutAdapter;
  154. private DatabaseHelper databaseHelper;
  155.  
  156.  
  157. @Override
  158. protected void onCreate(@Nullable Bundle savedInstanceState) {
  159. super.onCreate(savedInstanceState);
  160. setContentView(R.layout.activty_get_all_workouts);
  161.  
  162. listView = (ListView) findViewById(R.id.lv);
  163.  
  164. databaseHelper = new DatabaseHelper(this);
  165.  
  166. workoutArrayList = databaseHelper.getAllWokouts();
  167.  
  168. workoutAdapter = new WorkoutAdapter(this,workoutArrayList);
  169. listView.setAdapter(workoutAdapter);
  170.  
  171. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  172. @Override
  173. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  174. Intent intent = new Intent(GetAllWorkoutsActivity.this,AddExercises.class);
  175. intent.putExtra("workout_name", workoutArrayList.get(position));
  176. startActivity(intent);
  177. }
  178. });
  179.  
  180.  
  181. }
  182.  
  183. private ListView listView;
  184. private ArrayList<Exercise> exerciseArrayList;
  185. private ExerciseAdapter exerciseAdapter;
  186. private DatabaseHelper databaseHelper;
  187.  
  188. @Override
  189. protected void onCreate(Bundle savedInstanceState) {
  190. super.onCreate(savedInstanceState);
  191. setContentView(R.layout.activity_get_all_exercises);
  192.  
  193.  
  194.  
  195. listView = (ListView) findViewById(R.id.lv2);
  196.  
  197. databaseHelper = new DatabaseHelper(this);
  198.  
  199. exerciseArrayList = databaseHelper.getAllExercise();
  200.  
  201. exerciseAdapter = new ExerciseAdapter(this,exerciseArrayList);
  202. listView.setAdapter(exerciseAdapter);
  203.  
  204. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  205. @Override
  206. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  207. Intent intent = new Intent(GetAllExercisesActivity.this,AddExercises.class);
  208. intent.putExtra("user", exerciseArrayList.get(position));
  209. startActivity(intent);
  210. }
  211. });
  212.  
  213.  
  214. }
  215. }
Add Comment
Please, Sign In to add comment