Using @OnCheckedChanged (ButterKnife) with radioGroup gives error in android
Asked Answered
J

2

16

i recently integrated butterknife in my android project, and now i am trying to use @OnCheckedChanged annotation for radiogroup. but getting error of not giving callback. So what is the right method to call and get checkedId or this one is for radiobutton only and not for radiogroup.

@OnCheckedChanged(R.id.gendergroupid)
void onGenderSelected(RadioGroup group, int checkedId){
    switch(checkedId){
        case R.id.maleid:
            maleid.setEnabled(true);
            maleid.setChecked(true);
            break;
        case R.id.femaleid:
            femaleid.setEnabled(true);
            femaleid.setChecked(true);
            break;
        case R.id.bothid:
            bothid.setEnabled(true);
            bothid.setChecked(true);
            break;
    }
}

Gives me error

BloError:(89, 10) error: Unable to match @OnCheckedChanged method arguments.

Parameter #1: android.widget.RadioGroup did not match any listener parameters

Parameter #2: int did not match any listener parameters

Methods may have up to 2 parameter(s):

android.widget.CompoundButton boolean

These may be listed in any order but will be searched for from top to bottom.ckquote

Juanitajuanne answered 9/5, 2017 at 12:25 Comment(1)
both genders, my favouriteArborescent
S
53

According the specification, this annotation needs to be used with 2 parameters, a CompoundButton and a boolean, so if you really want to use this listener, you have to change it like this:

@OnCheckedChanged(R.id.gendergroupid)
void onGenderSelected(CompoundButton button, boolean checked) {
   //do your stuff.
}

I think in your case this listener doesn't work, so you can use another implementation like:

@OnClick({R.id.radio_1, R.id.radio_2}) 
public void onRadioButtonClicked(RadioButton radioButton) {
    // Is the button now checked?
    boolean checked = radioButton.isChecked();

    // Check which radio button was clicked
    switch (radioButton.getId()) {
      case R.id.radio_1:
        if (checked) {
          // 1 clicked
        }
        break;
      case R.id.radio_2:
        if (checked) {
          // 2 clicked
        }
        break;
    }
}
Soho answered 9/5, 2017 at 12:35 Comment(3)
Trying to use the first approach: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.CompoundButtonParthenogenesis
A CompoundButton is a single button, not a group like RadioGroup.Soho
RadioGroup extends LinearLayout, if you use @OnCheckedChange butterknife tryes to cast "RadioGroup" to "CompoundButton". This works only for "RadioButton" so you need to use a list of the radio buttons ids inside the radio group instead of the radio group itself.Warrantor
I
17

this worked for me

@OnCheckedChanged({R.id.radio_button1, R.id.radio_button2})
public void onRadioButtonCheckChanged(CompoundButton button, boolean checked) {
        if(checked) {
            switch (button.getId()) {
                case R.id.radio_button1:
                    // do stuff
                    break;
                case R.id.radio_button2:
                    // do stuff
                    break;
            }
        }
    }
Illfated answered 16/6, 2017 at 4:34 Comment(2)
@vuhung3990 you may have to remove the onClick implementation so it will trigger only onceIllfated
hi @Seph Remotigue, this is my mistake if radiogroup have 2 radiobutton, it will trigger 1 for notify button 1 check, 1 for notify button 2 uncheck but you have a conditional checked will only trigger 1 time, you are rightMakeup

© 2022 - 2024 — McMap. All rights reserved.