【问题标题】:store value from sqlite android to radio button将值从 sqlite android 存储到单选按钮
【发布时间】:2016-07-12 04:31:49
【问题描述】:

在我的应用程序中,用户可以进行 CRUD 操作。 当用户输入数据时,有 13 个单选组(选择的值是字符串格式)。完全输入数据后,用户可以将其保存到 sqlite 中。我的问题是,当用户单击编辑按钮时,我想知道返回所选单选按钮(用户之前检查过)的代码可以自动检查。

MainActivity 中的代码:

rg1 = (RadioGroup) findViewById(R.id.rg1);
     rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
     {
         public void onCheckedChanged(RadioGroup group, int checkedId) {
             switch(checkedId){
                 case R.id.rb_large:
                     palm_size="1";

                     // do operations specific to this selection
                 break;

                 case R.id.rb_average:
                     palm_size="2";

                     // do operations specific to this selection
                 break;

                 case R.id.rb_small:
                     // do operations specific to this selection
                     palm_size="3";

                 break;

                 case R.id.rb_vsmall:
                     // do operations specific to this selection
                     palm_size="4";

                 break;

             }
}
     });

获取字符串并在Edittext中显示的代码..

 protected void onResume(){

            super.onResume();
            i=getIntent();
            mark=i.getStringExtra("mark");
            //jika berasal dari edit, maka tampilkan

            if(mark!=null){
                //id=i.getStringExtra("id");
                et_estate.setText(i.getStringExtra("estate_name"));
                et_manuring.setText(i.getStringExtra("manuring_block"));
                et_year.setText(i.getStringExtra("year"));
                et_record.setText(i.getStringExtra("record_date"));

                et_bare.setText(i.getStringExtra("bare_soil"));
                et_legume.setText(i.getStringExtra("legumes"));
                et_lightgrass.setText(i.getStringExtra("light_grasses"));
                et_fern.setText(i.getStringExtra("ferns"));
                et_competitve.setText(i.getStringExtra("competitive"));
                et_remarks_competitive.setText(i.getStringExtra("competitive_weed"));

                //rg1=i.getStringExtra("yellowing");

                et_remarks.setText(i.getStringExtra("remarks"));

                id_assm=i.getStringExtra("id_assm");

            }


        }

如何将此代码选择的单选按钮恢复到单选组(rg1)? 帮我搞定。 谢谢

【问题讨论】:

    标签: android radio-button radio-group


    【解决方案1】:

    您可以保存RadioButtonid 而不是value
    然后您可以通过

    恢复选定的单选按钮
    intent.putExtra("idOfYourSelectedRadioButton", idOfYourSelectedRadioButton);  
    

    rg1.check(i.getIntExtra("idOfYourSelectedRadioButton", R.id.idOfDefaultRadioButton));
    

    【讨论】:

    • 还是不行..请让我知道另一个代码..我坚持下去
    【解决方案2】:

    我没有正确理解您的问题,但您似乎想以编程方式检查单选按钮。也许这会有所帮助

    onResume :

    rg1.check(R.id.rb_small)
    

    其中 R.id.rb_small 可能是您要恢复的选中值

    【讨论】:

    • 我的意思是如何恢复无线电组中的选定值。保存到 sqlite 中的值,当用户单击编辑按钮时,在用户第一次输入时自动选择单选组中的值 @nishan khadka
    【解决方案3】:
     public void onCheckedChanged(RadioGroup group, int checkedId) {
             switch(checkedId){
                 case R.id.rb_large:
                     palm_size="1";
    
                       String rb_large="true";
                     // do operations specific to this selection
                 break;
    
                 case R.id.rb_average:
                     palm_size="2";
                        String rb_average="true";
                     // do operations specific to this selection
                 break;
    
                 case R.id.rb_small:
                     // do operations specific to this selection
                     palm_size="3";
                    String rb_small="true"
                 break;
    
                 case R.id.rb_vsmall:
                     // do operations specific to this selection
                     palm_size="4";
                    String rb_vsmall="true"
                 break;
    
             }
    

    这些字符串发送到第二个活动,然后检查哪个字符串为真,这些字符串为真表示选中单选按钮,然后在 oncreate 中使用 setChecked(true) 方法自动为真

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多