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.