Jsin
Jsin

Reputation: 23

OnCheckedChangeListener conflict with checkBox vs radioGroup

I am working through a online tutorial building a tip calculator. In the video the guy shows how to build an inline click listener. I am trying to build click listeners for a checkbox and radio group. However, there seems to be a conflict that I'm having trouble resolving.

Here is the setup for the checkbox change listener..

friendlyCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            checklistValues[0] = (friendlyCheckBox.isChecked())?4:0;

            setTipFromWaitressChecklist();
            updateTipFinalBill();

        }

    });

and here is the setup for the radioGroup change listener..

howHotRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(RadioGroup group, int checkedId) {


        }


    });

I'm getting this error with the radioGroup change listener

The method setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener) in the type RadioGroup is not applicable for the arguments (new CompoundButton.OnCheckedChangeListener(){})

After doing some research I came to this thread.. Reasons for receiving "RadioGroup is not applicable for the arguments"

Which tells me to import this

import android.widget.RadioGroup.OnCheckedChangeListener;

When I do import that I get this error

import android.widget.RadioGroup.OnCheckedChangeListener collides with another import statement

I believe that these are the two conflicting imports but when I replace one with the other my error just moves from my checkbox to my radioGroup.

import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

Does anyone know how to resolve this?

Upvotes: 1

Views: 2565

Answers (1)

matiash
matiash

Reputation: 55340

Don't import any of them, just the corresponding outer classes and then use the inner class name when creating the anonymous listener, i.e.

import android.widget.CompoundButton;
import android.widget.RadioGroup;

and then

howHotRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() ...
friendlyCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() ...

Upvotes: 6

Related Questions