0

I have a list with each row having a radio button. I'm using the folowing code to toggle between them(TEMP is a static variable used to keep track of the element which has been selected, so that when listview refreshes the view I'm able to select it again) :

public void onClickRadioButton(View view) {
    final int position = listView.getPositionForView(view);
    View rowElement = ((View) view.getParent());

    // uncheck previous checked button.
    if (listRadioButton != null)
        listRadioButton.setChecked(false);

    // assign to the variable the new one
    listRadioButton = (RadioButton) view;
    // find if the new one is checked or not, and set "listIndex"

    if (listRadioButton.isChecked()) {
        listIndex = ((ListView) rowElement.getParent())
                .getPositionForView(rowElement);
        TEMP = listIndex;
    } else {
        listRadioButton = null;
        listIndex = -1;
        TEMP = listIndex;
    }

    System.out.println("list index  :  " + listIndex);
}

enter code here

This is the getView method of adapter class:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.manage_parameter_list_element,
            parent, false);
    TextView textView = (TextView) rowView
            .findViewById(R.id.parameter_textView);
    textView.setText("something");

    TextView textView2 = (TextView) rowView
            .findViewById(R.id.parameter_range_textView);
    textView2.setText("something more");

    if(position == SelectParameterActivity.TEMP)
            // SelectParameterActivity is the class whose code I've written above
    {
    ((RadioButton) rowView.findViewById(R.id.parameter_radioButton))
    .performClick();
    }

    return rowView;
}

Under normal conditions the switch between radio buttons is fine

Now the problem is, consider this scenario: I select option1....move down(so that option1 is not on screen anymore)....move up(option1 is visible again)...select option2(or any other apart from 1st) Now the 1st option does'nt get deselected..

To deselect option1 I have to click on it twice.

FYI I've tried the performClick() method which does not work due to IllegalSateException.

Pranav Mahajan
  • 2,048
  • 2
  • 23
  • 36

1 Answers1

0

Every time getView() is called you inflate a new View. This method gets called a lot! You cannot rely on the state of your radio button being stored/saved in the view. When the user clicks on a radio button, you need to save this information as part of your data, not in the view. Then, in getView() you need to set the state of the radio button based on the information that you have saved in the data.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I actually did that as you can see from the code. I saved the index of the radio button in a static variable TEMP. I'm using that variable to inflate the view from getView() in Adapter class. And the state of the button is actually restored. The problem is in the function onClickRadioButton(View view)..... listRadioButton.setChecked(false); does'nt respond for the row that has been restored... It still functions fine for the remaining rows but does'nt seem to respond for the row that has been restored... Any help would be appreciated – Pranav Mahajan Oct 31 '13 at 05:01
  • You need to call `setChecked()` on the `RadioButton` in `getView()` to reflect the current state. – David Wasser Oct 31 '13 at 08:10
  • When you call `listRadioButton.setChecked(false)` in `onClickRadioButton()` you are referencing a `View` that you previously saved in the previous call to `onClickRadioButton()`. However, that View is gone. It is no longer on-screen, because in the meantime Android has called `getView()` for that row and you've inflated and provided a new `View`. You are just going about this the wrong way. You cannot keep state in the **`View`**, you must keep state in the **data**. – David Wasser Oct 31 '13 at 08:14
  • onClickRadioButton() is actually working... like you said I've stored the selected button in listRadioButton(the data you're talking about) of type RadioButton.. In the adapter class instead of performClick() I'm using setCheched(false) currently...the problem comes when I move the list down n then up....onClickRadioButton() function is always working until I move the list down n up... PS: http://stackoverflow.com/questions/4250599/android-listview-with-radiobutton-in-singlechoice-mode-and-a-custom-row-layout .... the post by AlexKots(3rd post).... see the comment there – Pranav Mahajan Oct 31 '13 at 09:02
  • I'm not keeping the state in a view... I'm keeping it in a static variable TEMP... the view is simply using that static variable to enable the previously selected RadioButton – Pranav Mahajan Oct 31 '13 at 09:14
  • Try calling `notifyDataSetChanged()` on the adapter in your `onClickRadioButton()` method, to ensure that the adapter knows that it needs to update all the views. – David Wasser Oct 31 '13 at 09:47
  • It worked, but partially...Sometimes when I click on a radio button, it doesn't get selected , the previous one gets deselected. So I have to click again. And I think refreshing the entire list for one radio button click is not a very efficient thing to do... But still thanks a lot – Pranav Mahajan Oct 31 '13 at 10:47
  • If you still have any better idea then please tell me.... And if you have the time to actually see my code, that would be out of this world helpful. – Pranav Mahajan Oct 31 '13 at 10:49