0

I create a custom adapter where each row includes a radio button. Only one radio button can be selected at a time. Up until now I have always added a onClick (or onTouch) event handler to the listview and then gone through all the items (stored in my ArrayList) to determine which item was last selected, then uncheck it (if it's different than the currently selected one), then mark the item associated with the currently selected row to indicate that it has been selected and check the radio button for that item.

This seems kind of a long way of doing things. Is there a simple way to have Android just automatically uncheck whatever radio button is currently selected and then select the newly selected one?

Johann
  • 27,536
  • 39
  • 165
  • 279

2 Answers2

1

I would suggest you only keep track of currently selected item and call adapter.notifyDataSetChange() on radio button click, this would refresh view and uncheck previously selected radio buttons.

In adapter you have to write something like this:

View getView(...) {
    /////// initialize views

    if (getItemId(position) == selectedId) {
        radioButton.setChecked(true);
    } else {
        radioButton.setChecked(false);
    }

    //////////
}

This is not precise code, but I hope you got the idea.

Desert
  • 2,293
  • 15
  • 16
0

May this help you.

http://custom-android-dn.blogspot.in/2012/12/how-to-use-and-custom-radio-button-in_23.html

Radio buttons are usually grouped by Radio Group.When one RadioButton within a group is selected, all others are automatically deselected.

Priyanka
  • 677
  • 5
  • 20