我使用自定义适配器在每个行上创建了一个带有radioButton的自定义ListView。我想在检查其父视图(列表项)时以编程方式检查单选按钮,然后取消选中其他ListView项中的其他RadioButtons。任何人都可以提供解决方案吗?
更新
这是我的getView()
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout v = null;
final Item i = items.get(position);
EntryItem ei = (EntryItem) i;
v = (LinearLayout) vi.inflate(R.layout.list_item_entry, null);
final TextView title = (TextView) v
.findViewById(R.id.list_item_entry_title);
final TextView subtitle = (TextView) v
.findViewById(R.id.list_item_entry_summary);
RadioButton radioButton = new RadioButton(context);
radioButton.setFocusable(false);
radioButton.setFocusableInTouchMode(false);
v.addView(radioButton);
if (title != null)
title.setText(ei.title);
if (subtitle != null)
subtitle.setText(ei.subtitle);
convertView = v;
return convertView;
}
答案 0 :(得分:4)
如果应选择单个项目,则可以使用An int变量,初始化为-1;
int index =-1;
在getView方法中添加一行:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout v = null;
final Item i = items.get(position);
EntryItem ei = (EntryItem) i;
v = (LinearLayout) vi.inflate(R.layout.list_item_entry, null);
final TextView title = (TextView) v
.findViewById(R.id.list_item_entry_title);
final TextView subtitle = (TextView) v
.findViewById(R.id.list_item_entry_summary);
RadioButton radioButton = new RadioButton(context);
radioButton.setFocusable(false);
radioButton.setFocusableInTouchMode(false);
radioButton.setChecked(index==position);
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
}
});
v.addView(radioButton);
}
if (title != null)
title.setText(ei.title);
if (subtitle != null)
subtitle.setText(ei.subtitle);
}
convertView = v;
return convertView;
}
并且在侦听器的OnItemClick方法中,执行:
index=position;
adapter.notifyDataSetInvalidated();
答案 1 :(得分:2)
要重置单选按钮,请在getView
方法..
ListView.onsetOnItemClickListener()
中的:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get the radio button and check it
view.findViewById(); ....
}
});
答案 2 :(得分:1)
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
以上代码来自android.R.layout.simple_spinner_dropdown_item
它完全符合您的要求。可能这对你有帮助。
答案 3 :(得分:1)
您可以在列表视图的点击事件中管理它...在setOnClickListener事件中,您可以通过视图获取Radiobutton ..然后您可以管理它