2

I am creating an activity which contains 7 RadioGroups each with 5 RadioButtons. My problem is that I am getting selected index of only one RadioGroup (lvl_mon) and the other 6 are returning -1 however they are selected. My code is :

lvl_mon = (RadioGroup)findViewById(R.id.lvl_monday);
lvl_tue = (RadioGroup)findViewById(R.id.lvl_tuesday);
lvl_wed = (RadioGroup)findViewById(R.id.lvl_wednesday);
lvl_thu = (RadioGroup)findViewById(R.id.lvl_thursday);
lvl_fri = (RadioGroup)findViewById(R.id.lvl_friday);
lvl_sat = (RadioGroup)findViewById(R.id.lvl_saturday);
lvl_sun = (RadioGroup)findViewById(R.id.lvl_sunday);

int index_mon = lvl_mon.indexOfChild(findViewById(lvl_mon.getCheckedRadioButtonId()));
int index_tue = lvl_tue.indexOfChild(findViewById(lvl_tue.getCheckedRadioButtonId()));
int index_wed = lvl_wed.indexOfChild(findViewById(lvl_wed.getCheckedRadioButtonId()));
int index_thu = lvl_thu.indexOfChild(findViewById(lvl_thu.getCheckedRadioButtonId()));
int index_fri = lvl_fri.indexOfChild(findViewById(lvl_fri.getCheckedRadioButtonId()));
int index_sat = lvl_sat.indexOfChild(findViewById(lvl_sat.getCheckedRadioButtonId()));
int index_sun = lvl_sun.indexOfChild(findViewById(lvl_sun.getCheckedRadioButtonId()));

enter image description here

Shahzeb
  • 3,696
  • 4
  • 28
  • 47

4 Answers4

1

If you have buttons inside of your radio groups then you can do the following:

RadioGroup lvl_mon = (RadioGroup) findViewById(R.id.lvl_monday);
String selected_mon = ((RadioButton) findViewById(lvl_mon .getCheckedRadioButtonId())).getText().toString();
devmiles.com
  • 9,895
  • 5
  • 31
  • 47
1

I figured out the problem , I had same ids of RadioButtons in each RadioGroup so I changed the ids plus cleaned the project. This fixed the issue

Shahzeb
  • 3,696
  • 4
  • 28
  • 47
0

Have you tried like this

lvl_mon = (RadioGroup)findViewById(R.id.lvl_monday);
RadioButton lvl_mon1;

     lvl_mon.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

           lvl_mon1= (RadioButton)findViewById(checkedId);

           switch(checkedId){
           case R.id.radio0:
              // do somthings
           break;

           case R.id.radio1:
           .......

          }
        }
    });
Jamil
  • 5,457
  • 4
  • 26
  • 29
0
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);

This may help!!

Jagdish
  • 2,418
  • 4
  • 25
  • 51
  • isn't it the same as what I am doing ? – Shahzeb Sep 08 '14 at 12:32
  • You want to get the index of selected radio button of radio group at run time. right? – Jagdish Sep 08 '14 at 13:00
  • Try with getting at run time on checked changed event ... as well log it.. so, you will get exact idea. I am developing same type of application and faced the same -1 index. – Jagdish Sep 08 '14 at 13:40
  • I figured out the problem. I was having same ids for RadioButtons in each RadioGroup. I have changed the id of each RadioButton plus cleaned the project – Shahzeb Sep 08 '14 at 13:57