user2122397
user2122397

Reputation:

could not be able to eliminate error in android

to all I'm working on android and want to use both RadioGroup and CheckBox in simple app, and I want to use setOnCheckedChangeListener() for both but I'm facing error saying that import collides. My code is :

public class MainActivity extends Activity {

RadioGroup radioGroup;
CheckBox checkBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
    checkBox = (CheckBox) findViewById(R.id.checkBox1);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            int option = radioGroup.getCheckedRadioButtonId();
            if(option == R.id.radio0){
                Toast.makeText(MainActivity.this, "You Selected Java is Best", Toast.LENGTH_LONG).show();
            }
            else if(option == R.id.radio1){
                Toast.makeText(MainActivity.this, "You Selected C++ is Best", Toast.LENGTH_LONG).show();
            }
            else{
                Toast.makeText(MainActivity.this, "You Selected C# is Best", Toast.LENGTH_LONG).show();
            }
        }
    });

    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked){
                Toast.makeText(MainActivity.this, "You Selected Yes", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(MainActivity.this, "You Un-selected Yes", Toast.LENGTH_LONG).show();
            }
        }
    });

}

Edit

My Imports are :

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;// this one collides with

import android.widget.RadioGroup;

import android.widget.Toast;

import android.widget.RadioGroup.OnCheckedChangeListener;// this one

Now I'm unable to fix this please tell me what should I do. Thanks in Advance...

Upvotes: 1

Views: 165

Answers (1)

Sam
Sam

Reputation: 86948

There are two different OnCheckedChangeListeners, let the compiler know which version you want to use:

RadioGroup uses RadioGroup.OnCheckedChangeListener:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//                                        ^^^^^^^^^^^

CheckBox uses CompoundButton.OnCheckedChangeListener:

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                                      ^^^^^^^^^^^^^^^

Upvotes: 3

Related Questions