Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 836
Niki Avatar asked May 09 '17 12:05

Niki


2 Answers

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;
    }
}
like image 184
Luiz Fernando Salvaterra Avatar answered Sep 21 '22 18:09

Luiz Fernando Salvaterra


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;
            }
        }
    }
like image 22
Seph Remotigue Avatar answered Sep 21 '22 18:09

Seph Remotigue