Chance212
Chance212

Reputation: 31

Android: Populate TextView with row results (sqlite) from itemOnSelected Spinner

I would like to use a spinner as a menu to be able to select the name of a workout, upon selection the TextViews (x4) will be populated with the exercises relevant to that name. Currently the Spinner loads the workout names and have got as far as loading the first workout into each of the TextView's onCreation.

There are no errors when the app is launched, but the other rows don't load into the TextViews when the the new workout name is selected.. One can only assume I have made an error in the logic.

Question: If it is an error in the logic can someone identify it and point me in the direction please?

begin.java

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_begin);

    loadSpinnerData();
    refreshTextView();



}

   public void loadTextView(){
    DataHelper dataHelper = new DataHelper(this);
    ArrayList<String> exercises = dataHelper.getExercises();



          ((TextView) findViewById(R.id.exercise1)).setText(exercises.get(0));

          ((TextView) findViewById(R.id.exercise2)).setText(exercises.get(1));

          ((TextView) findViewById(R.id.exercise3)).setText(exercises.get(2));

          ((TextView) findViewById(R.id.exercise4)).setText(exercises.get(3));


  }
    public void refreshTextView(){
    Spinner databaseR = (Spinner) findViewById(R.id.databaseR);
    databaseR.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            loadTextView();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

DataHelper.java

  public  String Create_ex="CREATE TABLE ExerciserEasy"
        + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, Workout VARCHAR, ExerciseOne VARCHAR,"
        +"ExerciseTwo VARCHAR, ExerciseThree VARCHAR, ExerciseFour VARCHAR );"

   public ArrayList<String> getExercises(){
    ArrayList<String> list = new ArrayList<String>();
    SQLiteDatabase db = this.getReadableDatabase();
    db.beginTransaction();
    try {
        String selectQuery = " Select * from " + Table_ex1;
        Cursor cursor = db.rawQuery(selectQuery, null);
        if(cursor.moveToFirst()){
            do{
                list.add(cursor.getString(2));
                list.add(cursor.getString(3));
                list.add(cursor.getString(4));
                list.add(cursor.getString(5));
                Log.d("HIITtimer", ""+cursor.getCount());

            }while(cursor.moveToNext());
        }
        db.setTransactionSuccessful();
    }catch (Exception e){
        e.printStackTrace();
    }finally {

        db.endTransaction();
        db.close();


    }
    return list;


}

Upvotes: 1

Views: 160

Answers (1)

Yazan
Yazan

Reputation: 6082

first you need to pass the selected workout name to loadTextView() so you can pass it to getExercises() and query the database based on that name, other code looks fine,

1- add parameter workoutName to getExercises(), and use it in WHERE clause

public ArrayList<String> getExercises(String workoutName){
    //codes ...
    String selectQuery = " Select * from " + Table_ex1 + " WHERE Workout ='" workoutName + "'";
    //codes ...
}

Note: you may want to use SQLiteDatabase#query() instead of row query?

2- add parameter workoutName to loadTextView(), and pass it to getExercises():

public void loadTextView(String workoutName){
    //code ...
    ArrayList<String> exercises = dataHelper.getExercises(workoutName);
    //code ...
}

3- pass selected workout from spinner's setOnItemSelectedListener():

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    loadTextView(databaseR.getSelectedItem().toString());
}

now when item is selected, loadTextView() is called, telling the method which workout was selected, and query database to get the record related to that workout name.

Upvotes: 1

Related Questions