簡體   English   中英

Android:使用來自 itemOnSelected Spinner 的行結果(sqlite)填充 TextView

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

我想使用微調器作為菜單,以便能夠選擇鍛煉的名稱,選擇后TextView (x4) 將填充與該名稱相關的練習。 目前 Spinner 加載了鍛煉名稱,並且已經將第一個鍛煉加載到每個TextViewonCreation

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.

問題:如果這是邏輯錯誤,有人可以識別它並指出方向嗎?

開始.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) {
        }
    });

數據助手

  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;


}

首先,您需要將選定的鍛煉名稱傳遞給loadTextView()以便您可以將其傳遞給getExercises()並根據該名稱查詢數據庫,其他代碼看起來不錯,

1- 將參數workoutName添加到getExercises() ,並在WHERE子句中使用它

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

注意:您可能想使用SQLiteDatabase#query()而不是行查詢?

2- 將參數workoutName添加到loadTextView() ,並將其傳遞給getExercises()

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

3-通過 Spinner 的setOnItemSelectedListener()選擇的鍛煉:

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

現在當 item 被選中時, loadTextView()被調用,告訴方法選擇了哪個鍛煉,並查詢數據庫以獲取與該鍛煉名稱相關的記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM