【问题标题】:Custom Listview with Radiobutton, selected several .. Android带有 Radiobutton 的自定义 Listview,选择了几个 .. Android
【发布时间】:2014-11-10 12:38:56
【问题描述】:

我有一个自定义列表视图,如下图所示。但是要选择第一个单选按钮,也会选择滚动结束后的单选按钮,根据 eh read 那是通过使用android回收视图。我尝试了几种解决方案,但都没有成功..如果有人也一样,想看看我是否可以提供帮助..这是我的 Apadapter 的代码,你看看..干杯...!!

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

@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;
}

【问题讨论】:

  • 删除 'if (rowView == null) {' 并检查。
  • wooooooooooooooooowww,很简单的事情解决了我的问题,但不仅如此,还保存了 arrayList 中单选按钮的状态
  • 但这不是最好的方法,如果您的列表增加,它可能会引发 ANR。
  • 那我需要做什么?什么是最好的方法
  • Romadja 建议是应该如何完成的。

标签: android listview


【解决方案1】:

感谢回复的人。阅读我收到的回复,我找到了解决方案。

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

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;
}

}

【讨论】:

    【解决方案2】:

    您可以将检查的值存储在模型中。我看到你使用列表来存储一些数据,所以你也可以创建一个选中的列表,然后在getView方法中使用它:

    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));
    

    【讨论】:

    • 实现你的代码。而且我没有选择 2 个单选按钮,但现在当我滚动并返回第一个问题时。单选按钮不再被选中。其实我从来没有遇到过这个问题,现在我意识到我什么都不知道。
    猜你喜欢
    • 2014-05-03
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多