我发现这段代码效果非常好,我必须使用RadioButton创建一些非常相似的东西。我需要改变什么?
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找到了所有这些代码。
答案 0 :(得分:1)
我想你会在这个已经回答的问题中找到所有必需的信息:
listview with radio group error
@Dante的接受答案包括您重写自定义数组适配器以使用radiobuttons所需的所有代码。
修改
根据您的评论,我不确定您是否真的知道如何使用自定义适配器编写自定义列表,查看持有者和对象。
Romain Guy详细解释了listview
http://www.google.com/events/io/2010/sessions/world-of-listview-android.html
和相关的pdf
http://dl.google.com/googleio/2010/android-world-of-listview-android.pdf
一旦你handel customadapter viewholder等你只需要让你的列表实现可检查。例如这里
How to use RadioGroup in ListView custom adapter?
如果您不知道如何设置自定义列表视图,这是一个示例,但它与您的问题没有直接关系:
// The Adapter
class myObjectAdapter extends ArrayAdapter<myObject> {
myObjectAdapter() {
super(MyActivity.this, R.layout.my_row_layout,
listItems);
}
public View getView(int position, View convertView, ViewGroup parent) {
myObjectHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.my_row_layout,
parent, false);
holder = new myObjectHolder(convertView);
convertView.setTag(holder);
} else {
holder = (myObjectHolder) convertView.getTag();
}
holder.populateFrom(listItems.get(position));
return (convertView);
}
}
// The Holder
static class myObjectHolder {
private TextView myText = null;
myObjectHolder(View row) {
myText = (TextView) row.findViewById(R.id.MyTextView);
}
void populateFrom(myObject r) {
myText.setText(r.getText());
}
}
// The Object
class myObject {
public final String myText;
public myObject(String myText) {
this.myText = myText;
}
public String getText() {
return myText;
}
}
假设您有一个名为my_row_layout的xml文件,其中包含一个名为MyTextView的文本视图。在您的情况下,您将使用单选按钮替换textview,并根据上面给出的链接实现可检查,它将起作用。
希望有所帮助