RadioGroup onCheckedChanged function does not work

My app tracks restaurant server sales to help them on a budget. In an action showing past shifts, I created a RadioGroup under a ListView so that the user can display lunch, dinner, or both.

I implemented in the activity of RadioGroup.onCheckedChangeListener, but onCheckedChanged is never called. I also tried using an anonymous inner class as a listener, the same result.

I tried copying / changing the code from this answer: https://stackoverflow.com/a/166268/2126161 ... but when I added the @Override function to the callback function, the Eclipse compiler gave me an error (not a warning) that the method should override superclass, and the quick fix is ​​to remove the override.

I am sure the signatures are consistent, as they were made with Eclipse autocomplete and implement the methods. Then I followed the instructions for moving my java compiler from 1.5 to 1.6, and none of the above actions changed.

Here is the code that I think is appropriate:

 public class DataActivity extends ListActivity implements OnCheckedChangeListener{ RadioButton rbBoth; RadioButton rbDinnerOnly; RadioButton rbLunchOnly; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.database); // ... final RadioGroup rgGroup = (RadioGroup)findViewById(R.id.DataRadioGroup); rbBoth = (RadioButton)findViewById(R.id.RadioBoth); rbDinnerOnly = (RadioButton)findViewById(R.id.RadioDinnerOnly); rbLunchOnly = (RadioButton)findViewById(R.id.RadioLunchOnly); rgGroup.setOnCheckedChangeListener(this); populateAllShifts(); } // ... public void onCheckedChanged(RadioGroup group, int checkedId) { rbLunchOnly.setText("Click!"); Toast.makeText(getApplicationContext(), "Lunch Only", Toast.LENGTH_LONG).show(); if(group.getCheckedRadioButtonId() == R.id.RadioBoth){ populateAllShifts(); return; } if(group.getCheckedRadioButtonId() == R.id.RadioLunchOnly){ populatLunchShifts(); return; } if(group.getCheckedRadioButtonId() == R.id.RadioDinnerOnly){ populateDinnerShifts(); return; } } // ... } 

There is a ListView with a custom adapter in this class, but if my understanding and my XML are correct, RadioGroup should be outside the list:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/llDataLayout" android:weightSum="5" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ListView android:layout_weight="4" android:layout_width="fill_parent" android:id="@android:id/list" android:layout_height="wrap_content"> </ListView> <RadioGroup android:layout_weight="1" android:id="@+id/DataRadioGroup" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent"> <RadioButton android:text="Lunch and Dinner" android:textSize="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/RadioBoth"/> <RadioButton android:text="Dinner Only" android:textSize="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/RadioDinnerOnly"/> <RadioButton android:text="Lunch Only" android:textSize="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/RadioLunchOnly"/> </RadioGroup> </LinearLayout> 

Any ideas there?

+4
source share
4 answers

Found a problem, and the answer was not visible from the information I gave in my question. The problem was cool.

It started as my first project, and in an attempt to solve the database problem a few months ago, I still redefined onStart () and onRestart () with almost the same code in onCreate (). onCreate has changed over time, and these methods have not. As soon as I deleted them, the code worked fine.

I did not show these methods in the question. Excuse me!

Thank you all for your help! What should I do now? Delete a question?

+1
source

You already get checkedId as a parameter, a unique identifier for the newly installed switch.

 public void onCheckedChanged(RadioGroup group, int checkedId) { rbLunchOnly.setText("Click!"); Toast.makeText(getApplicationContext(), "Lunch Only", Toast.LENGTH_LONG).show(); switch(checkedId){ case R.id.RadioBoth : populateAllShifts(); break; //--other cases--- } 
+1
source

you should use this method:

 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked == true) { if(buttonView == radioA) tvInfo.setText("A"); else if(buttonView == radioB) tvInfo.setText("B"); else if(buttonView == radioC) tvInfo.setText("C"); } } 
0
source

it should work .. in ur xml. u can provide this interactive option for radio objects on android:clickable="true" in java ..

 public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.RadioBoth : populateAllShifts(); break; case R.id.RadioDinnerOnly: populatDinnerShifts(); break; //----- default: break; } 
0
source

Source: https://habr.com/ru/post/1440686/


All Articles