繁体   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