自定义Listview与单选按钮,选择几个.. Android

 2021-04-26 

Custom Listview with Radiobutton, selected several .. Android

我有一个自定义的Listview,如下图。但是要选择第一个单选按钮,还要在滚动结束后选择一个单选按钮,根据阅读,这是通过使用android回收视图来实现的。我尝试了几种解决方案,但没有成功。.如果有人一直都想看看我是否可以帮助..这是我的Apadapter的代码,请看一下..干杯... !!

链接图片> http://sia1.subirimagenes.net/img/2014/11/10/141110041729113463.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    if (rowView == null) {

        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.list_single, null, true);
        ViewHolder viewHolder = new ViewHolder();

        viewHolder.pregunta = (TextView) rowView.findViewById(R.id.texto_pregunta);
        viewHolder.rdo1 = (RadioButton) rowView.findViewById(R.id.radio0);

        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();
    holder.pregunta.setText((position + 1) +".-" + desc.get(position));
    holder.rdo1.setText(minimo.get(position));
    return rowView;
}

public static class ViewHolder {
    public TextView pregunta;
    public RadioButton rdo1;
    public RadioButton rdo2;
    public RadioButton rdo3;
}


感谢那些做出回应的人。阅读我收到的回复后,我找到了解决方法。

如果适配器和我有同样的问题,我将让适配器继续使用...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class CustomList extends ArrayAdapter<String> {

private final Activity context;
private final ArrayList<String> minimo;
private final ArrayList<String> maximo;

private ArrayList<Boolean> status = new ArrayList<Boolean>();

public CustomList(Activity context, ArrayList<String> min, ArrayList<String> max) {
    super(context, R.layout.list_single, min);
    this.context = context;
    this.minimo = min;
    this.maximo = max;

    //initialize all with false
    for (int i = 0; i < minimo.size(); i++) {
        status.add(false);
    }
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    LayoutInflater inflater = context.getLayoutInflater();
    rowView = inflater.inflate(R.layout.list_single, null, true);
    ViewHolder viewHolder = new ViewHolder();

    viewHolder.pregunta = (TextView) rowView.findViewById(R.id.texto_pregunta);
    viewHolder.rdo1 = (RadioButton) rowView.findViewById(R.id.radio0);
    rowView.setTag(viewHolder);

    ViewHolder holder = (ViewHolder) rowView.getTag();
    holder.pregunta.setText("Some Text Answer");
    holder.rdo1.setText(minimo.get(position));
    holder.rdo1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //change status when clicked
            status.set(position, isChecked);

            //verified status all radiobuttons
            for (int i = 0; i < status.size(); i++) {
                Log.v("Log","" + status.get(i));
            }
        }
    });
    holder.rdo1.setChecked(status.get(position));
    return rowView;
}

public static class ViewHolder {
    public TextView pregunta;
    public RadioButton rdo1;
    public RadioButton rdo2;
    public RadioButton rdo3;
}

} ??


您可以将检查的值存储在模型中。我看到您使用列表来存储一些数据,因此您还可以创建一个带有选中状态的列表,然后在getView方法中使用它:

1
2
3
4
5
6
7
8
9
private ArrayList<Boolean> checked;

holder.rdo1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checked.set(position, isChecked);
        }
});
holder.rdo1.setChecked(checked.get(position));