// Replace this code and try
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (GridView) findViewById(R.id.list);
adapter = new LazyAdapter(this, mStrings);
list.setAdapter(adapter);
RadioButton btnAll = (RadioButton) findViewById(R.id.btnAll);
btnCategories.setOnCheckedChangeListener(btnAllOnCheckedChangeListener);
RadioButton btnCategories = (RadioButton) findViewById(R.id.btnCategories);
btnCategories.setOnCheckedChangeListener(btnCategoriesOnCheckedChangeListener);
RadioButton btnPopular = (RadioButton) findViewById(R.id.btnPopular);
btnPopular.setOnCheckedChangeListener(btnPopularOnCheckedChangeListener);
RadioButton btnAbout = (RadioButton) findViewById(R.id.btnAbout);
btnAbout.setOnCheckedChangeListener(btnAboutOnCheckedChangeListener);
}
OnCheckedChangeListener Not responding correctly
-
29-06-2022 - |
Frage
I have set up 4 RadioButton's in an activity.
I'm having a problem where when I click on the "ALL" RadioButton, it shows the toast corresponding to the "CATEGORIES" RadioButton. Also, when I click on the "CATEGORIES" RadioButton, it displays the toast corresponding the "ABOUT" RadioButton. Then when I click on the "ABOUT" RadioButton, nothing displays.
I know it's something to do with some things not being directed correctly, but can't seem to find out what it is.
FYI: Later on I will be opening up activities with these RadioButtons, that's why I have't just done GetText and used just 1 OnCheckedChangeListener
Any help would be appreciated!
MainActivity:
public class MainActivity extends Activity {
GridView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (GridView) findViewById(R.id.list);
adapter = new LazyAdapter(this, mStrings);
list.setAdapter(adapter);
RadioButton radioButton;
radioButton = (RadioButton) findViewById(R.id.btnAll);
radioButton.setOnCheckedChangeListener(btnAllOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnAll);
radioButton
.setOnCheckedChangeListener(btnCategoriesOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnCategories);
radioButton
.setOnCheckedChangeListener(btnPopularOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnPopular);
radioButton.setOnCheckedChangeListener(btnAboutOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnAbout);
}
private CompoundButton.OnCheckedChangeListener btnAllOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened ALL tab", Toast.LENGTH_LONG).show();
}
}
};
private CompoundButton.OnCheckedChangeListener btnCategoriesOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened CATEGORIES tab", Toast.LENGTH_LONG).show();
}
}
};
private CompoundButton.OnCheckedChangeListener btnPopularOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened POPULAR tab", Toast.LENGTH_LONG).show();
}
}
};
private CompoundButton.OnCheckedChangeListener btnAboutOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened ABOUT tab", Toast.LENGTH_LONG).show();
}
}
};
Lösung
Andere Tipps
You repeat the All radio button twice. The code should be:
RadioButton radioButton;
radioButton = (RadioButton) findViewById(R.id.btnAll);
radioButton.setOnCheckedChangeListener(btnAllOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnCategories);
radioButton
.setOnCheckedChangeListener(btnCategoriesOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnPopular);
radioButton
.setOnCheckedChangeListener(btnPopularOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnAbout);
radioButton.setOnCheckedChangeListener(btnAboutOnCheckedChangeListener);
make single CompoundButton.OnCheckedChangeListener in this way:
private CompoundButton.OnCheckedChangeListener btnOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
switch buttonView{
case btn1:
//your logic
and so on
}
}
};
and apply this listener for all your buttons