各行にラジオボタンがあるリストがあります。次のコードを使用してそれらを切り替えています(TEMPは選択された要素を追跡するために使用される静的変数であるため、リストビューがビューを更新すると、再度選択できます):
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
これは、アダプター クラスの getView メソッドです。
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;
}
通常の状態では、ラジオ ボタン間の切り替えは問題ありません
ここでの問題は、次のシナリオを考えてみてください: オプション 1 を選択....下に移動 (オプション 1 が画面上に表示されないように)... 上に移動 (オプション 1 が再び表示される)... オプション 2 を選択1st から) 1st オプションの選択が解除されなくなりました..
option1 の選択を解除するには、それを 2 回クリックする必要があります。
参考までに、 IllegalSateException が原因で機能しない performClick() メソッドを試しました。