samedi 19 mars 2016

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

Hi guys it's my first time posting, please forgive me if it's a bit messy.. any pointers will be welcomed.

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 TextViews 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? Many thanks. Chance212

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;


}

Aucun commentaire:

Enregistrer un commentaire