Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有单选按钮的自定义Listview,已选择多个。。安卓_Android_Listview - Fatal编程技术网

Android 带有单选按钮的自定义Listview,已选择多个。。安卓

Android 带有单选按钮的自定义Listview,已选择多个。。安卓,android,listview,Android,Listview,我有一个自定义的Listview,如下图所示。但要选择第一个单选按钮,也会选择滚动结束后的单选按钮,这是通过使用android循环视图实现的。我尝试了几种解决办法,但没有成功。。如果有人也一样的话,我想看看我能不能帮上忙。。这是我的Apadapter的代码,你看一下。。干杯 链接图像> 可以将选中的值存储在模型中。我看到您使用列表来存储一些数据,因此您还可以使用checked再创建一个列表,然后在getView方法中使用它: private ArrayList<Boolean> ch

我有一个自定义的Listview,如下图所示。但要选择第一个单选按钮,也会选择滚动结束后的单选按钮,这是通过使用android循环视图实现的。我尝试了几种解决办法,但没有成功。。如果有人也一样的话,我想看看我能不能帮上忙。。这是我的Apadapter的代码,你看一下。。干杯

链接图像>


可以将选中的值存储在模型中。我看到您使用列表来存储一些数据,因此您还可以使用checked再创建一个列表,然后在
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));
private ArrayList已选中;
holder.rdo1.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
已检查。设置(位置,已检查);
}
});
holder.rdo1.setChecked(checked.get(position));

感谢那些回复的人。通过阅读我收到的回复,我找到了解决办法

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

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;
}
公共类CustomList扩展了ArrayAdapter{
私人最终活动背景;
私人最终ArrayList minimo;
私人终审法官马克西莫;
private ArrayList status=new ArrayList();
公共自定义列表(活动上下文、ArrayList最小值、ArrayList最大值){
超级(上下文、右布局、单列表、最小值);
this.context=上下文;
这是最小值=最小值;
这个最大值=最大值;
//用false初始化所有
对于(int i=0;i

}

Remove'if(行视图==null){'和check.woooooooww,简单的解决了我的问题,但不仅如此还保存了ArrayList中单选按钮的状态但这不是最好的方法,如果列表增长,它可能会抛出ANR。那么我需要做什么?最好的方法是什么Romadja建议是应该如何做的方法。实现你的代码。我不选择e 2单选按钮,但现在当我滚动并返回到第一个问题时。单选按钮不再被选中。实际上我从未遇到过这个问题,现在我意识到我一无所知。
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;
}