【问题标题】:How to retrieve to Radio button group in android studio with SQL-lite database如何使用 SQL-lite 数据库检索到 android studio 中的单选按钮组
【发布时间】:2020-02-10 06:02:30
【问题描述】:

我已尝试将数据检索到单选按钮组。数据插入没问题,但我需要一些帮助,以便将数据检索到单选按钮组。

 }
                else{
                    while(res.moveToNext()){
                        Log.d(res.getString(2),"id");
                        dob_et.setText(res.getString(2));
                        Gender.getCheckedRadioButtonId(res.getString(3));
                        pw_et.setText(res.getString(4));
                    }
                }
            }

【问题讨论】:

    标签: java android sqlite android-studio android-fragments


    【解决方案1】:

    您没有提供足够的代码来准确回答这个问题,但您似乎正在尝试使用 R.id. int 值进行存储和恢复?而且我认为这不是一个好主意,因为它们将来可能会改变。

    当此方法不接受任何参数时,您似乎还试图传递 getCheckedRadioButtonId 参数。

    https://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId()

    有两种方法可以在 RadioGroup 中设置值(我不得不假设 RadioButtons 和 RadioGroup 的 id)

    1) 间接

    RadioGroup gender = findViewById(R.id.gender);
    gender.check(R.id.male);
    
    

    1) 直接

    RadioButton male = findViewById(R.id.male);
    male.setChecked(true);
    
    

    所以从数据库代码中恢复值看起来像

    while(res.moveToNext()){
        String gender = res.getString(3)
        if (gender == "male") {
            RadioButton male = findViewById(R.id.male);
            male.setChecked(true);
        } else {
            RadioButton female = findViewById(R.id.female);
            female.setChecked(true);
        }
    }
    

    PS 如果你现在政治正确,你可能有 3 个“男”、“女”或“其他”单选按钮

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2017-03-28
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多