How to programmatically select RadioButton in action

In my project, I get the gender value from the database as 0 and 1.
0 for men and 1 for women.
Based on these values, I need to check the corresponding RadioButton in the RadioGroup.
If you press 0, the value of radio0 will be checked or radio1 will be checked.
I don't know how to test a switch based on these string values ​​...
This is the code I tried:

String gendd=ViewProfileActivity.participantview.get(4); System.out.println("Gender:::::"+gendd); male=(RadioButton)findViewById(R.id.radio0); female=(RadioButton)findViewById(R.id.radio1); if(gendd.equals("0")){ male.setSelected(true); female.setSelected(false); } else { male.setSelected(false); female.setSelected(true); } 

But that fails. Can anybody help me?

+8
android selected button radio
source share
2 answers

Try it.

  if(gendd.contains("0")) { male.setChecked(true); female.setChecked(false); } else { male.setChecked(false); female.setChecked(true); } 
+10
source share

You are using the check(radioButtonId) method check(radioButtonId) for the parent RadioGroup.

eg.

 radioGroup.check(gendd.equals("0") ? R.id.radio0 : R.id.radio1); 

(assuming the comparison is being performed since the types were not mentioned)

+9
source share

All Articles