将 sqlite android 中的值存储到单选按钮

store value from sqlite android to radio button

在我的应用程序中,用户可以进行 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)中? 帮我搞定。 谢谢

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

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

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

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

恢复时:

rg1.check(R.id.rb_small)

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

 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;

         }

这些字符串发送到第二个activity然后检查哪个字符串为真,那些字符串为真意味着单选按钮被选中,然后在oncreate中你使用setChecked(true)方法自动为真