0

I have an Activity which launches an AlertDialog with the following code. Unfortunately, the id that comes from the RadioGroup as checked is always the ID of the first radio button, no matter which radio button was actually checked.

I suspect the problem is that the view has already been disposed of when the OK button is pressed, but I don't know why the string would still work if that were the case.

Why would this happen, and what could I do to get the actual radio button checked?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View v = getLayoutInflater().inflate(R.layout.dialog_add_team, null);
builder.setView(v);
// Add the buttons
builder.setPositiveButton(R.string.ok,
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
            String name = ((EditText) v.findViewById(R.id.team_name)).getText()+"";
            if (name.equals(""))
                return;
            int checkedId=((RadioGroup)v.findViewById(R.id.radioGroup1))
                              .getCheckedRadioButtonId();
            teamAdded(name, checkedId);
        }
    });
    builder.setNegativeButton(R.string.cancel,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });

    AlertDialog dialog = builder.create();
    dialog.show();

}

Here is the XML for my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/team_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/teamname" >

        <requestFocus />
    </EditText>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/bothgenders" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/boys" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/girls" />
    </RadioGroup>

</LinearLayout>
Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65

1 Answers1

2

I think you should set setOnCheckedChangeListener to your radioGroup. You can use this answer. Hope this helps.

Community
  • 1
  • 1
ACengiz
  • 1,285
  • 17
  • 23
  • Do you know why what I'm doing doesn't work? I don't need to know when the check changes, only what position it is in when the user presses OK. – Jakob Weisblat Apr 27 '13 at 20:28
  • You can not detect the radio button clicked or not without a listener so you need a listener. In onCheckedChanged method of listener you can detect the selected radio button. – ACengiz Apr 27 '13 at 20:32
  • Then what is the `getCheckedRadioButtonId()` method for? – Jakob Weisblat Apr 27 '13 at 20:34
  • I have not used that method before and I made a quick search. I guess I found an alternative solution to your problem. http://stackoverflow.com/a/6441097/909497 may be answer of your question about getCheckedRadioButtonId(). Hope this helps. – ACengiz Apr 27 '13 at 20:46
  • can you edit your answer to reflect that last comment, which worked? – Jakob Weisblat Apr 27 '13 at 20:56
  • I reconsidered my answers and I think first one is correct. Because you can only get id of initial selected radioButton which is in radioButtonGroup. – ACengiz Apr 27 '13 at 21:54
  • but only one radio button *can* be selected at a time. – Jakob Weisblat Apr 27 '13 at 21:56
  • In radioButtonGroup, there should be an initial selected radioButton. You can get id of that radioButton by my second answer. But if you click another radioButton, selected radioButton changes and you have to use a listener to detect this event. You can also get id of new selected radioButton by using listener methods. – ACengiz Apr 27 '13 at 22:00
  • I implemented the linked answer, and it works even when the selected radio button changes. – Jakob Weisblat Apr 27 '13 at 22:01