我发现这个代码工作得很好,我必须创建一些非常相似,但与RadioButton。我需要改变什么?Android:与RadioButton ArrayAdapter
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
public class Model {
private String name;
private boolean selected;
public Model(String name) {
this.name = name;
selected = false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
“rowbuttonlayout.xml”。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30px" >
</TextView>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px" >
</CheckBox>
</RelativeLayout>
我发现这个website所有代码。
但丁的解决方案存在问题。他创建了5个RadioButton!我必须创建相同的RadioButton,因为我不知道数字。我能怎么做? – michoprogrammer 2012-02-21 14:52:09
好吧,我开始明白这个问题.. – michoprogrammer 2012-02-21 20:40:36
http://stackoverflow.com/questions/7329856/how-to-use-radiogroup-in-listview-custom-adapter < - 这篇文章不帮助我。 您的示例非常有用,此http://www.google.com/events/io/2010/sessions/world-of-listview-android.html有助于理解该问题。 现在我创建了与单选按钮相同的东西,但我不知道为什么可以检查很多按钮,而不仅仅是一个......我想问题是我没有设置radiogroup,但我不知道我不知道我可以在哪里设置它! 对不起新手问题,但我只用了2周的android ... – michoprogrammer 2012-02-21 20:47:39