ListView Holder复选框错误

时间:2014-05-17 02:15:42

标签: java android performance android-layout android-arrayadapter

我正在开发一个带有自定义布局和 arrayAdapter 的应用:

enter image description here

问题是当我检查一个方框时,应用会自动检查另外2个方​​框

我创建了一个列表,其中我放置了复选框的位置并显示没有错!我认为问题是当我执行回收

让我们查看一些代码;这是我的ArrayAdapter:

class VivzAdapter extends ArrayAdapter<String> implements OnCheckedChangeListener {
Context context;
int[] images;
String[] titlesArray, descrptionArray;
List<Integer> positions = new ArrayList<Integer>();

VivzAdapter(Context context, String[] titles, int[] images, String[] description) {

    super(context, R.layout.single_row, R.id.textView1, titles);
    this.context = context;
    this.images = images;
    this.titlesArray = titles;
    this.descrptionArray = description;
}

class MyViewHolder {
    ImageView myImage;
    TextView myTitle;
    TextView myDescription;
    CheckBox box;

    MyViewHolder(View v) {
        myImage = (ImageView) v.findViewById(R.id.imageView1);
        myTitle = (TextView) v.findViewById(R.id.textView1);
        myDescription = (TextView) v.findViewById(R.id.textView2);
        box = (CheckBox) v.findViewById(R.id.checkBox1);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    MyViewHolder holder = null;
    if (row == null) {
        // 1.ºtime
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //row contem RelativeLayout(root) em single_row.xml
        row = inflater.inflate(R.layout.single_row, parent, false);
        holder = new MyViewHolder(row);
        row.setTag(holder);
        //Log.d("VIVZ", "Creating a new Row");
    } else {
        //reciclamos aqui, qeremos usar antigo objecto holder
        holder = (MyViewHolder) row.getTag();
        //Log.d("VIVZ", "Recycling stuff");
    }
    holder.myImage.setImageResource(images[position]);
    holder.myTitle.setText(titlesArray[position]);
    holder.myDescription.setText(descrptionArray[position]);
    holder.box.setTag(position);
    holder.box.setOnCheckedChangeListener(this);
    return row;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
        int position = (Integer)buttonView.getTag();
        Toast.makeText(getContext(), ""+position, Toast.LENGTH_SHORT).show();
        positions.add(position);
        Log.d("VIVZ", "+ "+position);
    } else {
        int position = (Integer)buttonView.getTag();
        Toast.makeText(getContext(), ""+position, Toast.LENGTH_SHORT).show();
        Log.d("VIVZ", "- "+position);
        positions.remove(position);
    }
    Log.d("VIVZ", positions.toString());
}
}

3 个答案:

答案 0 :(得分:4)

我遇到了同样的问题并找到了解决方案。 创建一个大小与no相同的布尔数组。列表中的元素和每个列表项的初始值false。然后在getView中通过setId在Id中设置位置复选框。现在通过复选框的setChecked方法设置复选框的值从布尔数组。为复选框添加onClickListener并在此侦听器中直接更改布尔数组中的值而不是复选框值。 我希望这也能解决你的问题。

只需用以下代码替换您的适配器类:

class VivzAdapter extends ArrayAdapter<String> implements OnCheckedChangeListener {
Context context;
int[] images;
String[] titlesArray, descrptionArray;
List<Integer> positions = new ArrayList<Integer>();

ArrayList<Boolean> arrChecked;

VivzAdapter(Context context, String[] titles, int[] images, String[] description) {

super(context, R.layout.single_row, R.id.textView1, titles);
this.context = context;
this.images = images;
this.titlesArray = titles;
this.descrptionArray = description;

// initialize arrChecked boolean array and add checkbox value as false initially for each item of listview
arrChecked = new ArrayList<Boolean>();
for (int i = 0; i < titles.size(); i++) {
arrChecked.add(false);
}
}

class MyViewHolder {
ImageView myImage;
TextView myTitle;
TextView myDescription;
CheckBox box;

MyViewHolder(View v) {
    myImage = (ImageView) v.findViewById(R.id.imageView1);
    myTitle = (TextView) v.findViewById(R.id.textView1);
    myDescription = (TextView) v.findViewById(R.id.textView2);
    box = (CheckBox) v.findViewById(R.id.checkBox1);
}
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyViewHolder holder = null;
if (row == null) {
    // 1.ºtime
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //row contem RelativeLayout(root) em single_row.xml
    row = inflater.inflate(R.layout.single_row, parent, false);
    holder = new MyViewHolder(row);
    row.setTag(holder);
    //Log.d("VIVZ", "Creating a new Row");
} else {
    //reciclamos aqui, qeremos usar antigo objecto holder
    holder = (MyViewHolder) row.getTag();
    //Log.d("VIVZ", "Recycling stuff");
}
holder.myImage.setImageResource(images[position]);
holder.myTitle.setText(titlesArray[position]);
holder.myDescription.setText(descrptionArray[position]);

//set position as id
holder.box.setId(position);
//set onClickListener of checkbox rather than onCheckedChangeListener
holder.box.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int id = v.getId();

            if (arrChecked.get(id)) {
                //if checked, make it unchecked
                arrChecked.set(id, false);
            } else {
                //if unchecked, make it checked
                arrChecked.set(id, true);

            }

        }
    });

    //set the value of each checkbox from arrChecked boolean array
    holder.box.setChecked(arrChecked.get(position));

return row;
}
}

答案 1 :(得分:1)

getView方法结束时,您应该检查当前位置是否在位置数组中,这应该可以解决问题

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MyViewHolder holder = null;
        if (row == null) {
            // 1.ºtime
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            //row contem RelativeLayout(root) em single_row.xml
            row = inflater.inflate(R.layout.single_row, parent, false);
            holder = new MyViewHolder(row);
            row.setTag(holder);
            //Log.d("VIVZ", "Creating a new Row");
        } else {
            //reciclamos aqui, qeremos usar antigo objecto holder
            holder = (MyViewHolder) row.getTag();
            //Log.d("VIVZ", "Recycling stuff");
        }
        holder.myImage.setImageResource(images[position]);
        holder.myTitle.setText(titlesArray[position]);
        holder.myDescription.setText(descrptionArray[position]);
        holder.box.setTag(position);
        holder.box.setOnCheckedChangeListener(this);

    ////// Here you should update the checlbox status by checking if the current index is in the position array or not ////////////
boolean isFound = false;
for(int i =0;i<position.size();i++){
if(positions.get(i) == position){ // this line might need casting
isFound = true;
break;
}
holder.box.setChecked(isFound);
}
    return row;
}

答案 2 :(得分:1)

将此示例项目用于您的问题。链接下载完整项目 https://drive.google.com/file/d/0B6C9Pqrvc_CWZHFDcmxKR01rc3c/edit?usp=sharing