LeBeau
LeBeau

Reputation: 161

RadioGroup onCheckedChanged function won't fire

My app keeps track of restaurant servers' shift sales to help them budget. In the activity that displays past shifts, I've created a RadioGroup under the ListView so the user can choose to display lunch, dinner, or both.

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

I tried to copy/modify code from this answer: https://stackoverflow.com/a/9595528 ...but when I added the @Override to the callback function, the Eclipse compiler gave me an error (not warning) that the method must override a superclass, and the quick fix was to remove the override.

I'm pretty sure the signatures are match, as they were made with Eclipse's autocomplete and implement methods facilities. I then followed instructions to move my java compiler from 1.5 to 1.6, and none of the above listed behavior seemed to change.

Here's the code I think is relevant:

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 in this class with a custom adapter, but if my understanding and my XML are correct, the RadioGroup should be outside of 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 out there?

Upvotes: 4

Views: 6220

Answers (4)

LeBeau
LeBeau

Reputation: 161

Found the problem, and the answer wasn't visible from the info I gave in my question. The problem was cruft.

This started as my first project, and in a ham-handed attempt to solve a database problem months ago, I had extraneously overridden onStart() and onRestart() with pretty much the same code in onCreate(). onCreate has changed over time, and those methods did not. Once I deleted them, the code worked perfectly.

I hadn't shown those methods in the question. Sorry!

Thanks everyone for your help! What should I do now? Delete the question?

Upvotes: 1

mainu
mainu

Reputation: 448

it should work.. in ur xml. u can give that clickable option for radiobuttons by 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;
   }

Upvotes: 0

s.d
s.d

Reputation: 29436

You are already getting checkedId as a parameter, the unique identifier of the newly checked radio button.

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---

   }

Upvotes: 1

Nhật Minh &#212;n
Nhật Minh &#212;n

Reputation: 21

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");
    }
}

Upvotes: 0

Related Questions