gpt4 book ai didi

java - ListView Holder 检查复选框 Bug

转载 作者:行者123 更新时间:2023-12-01 08:02:59 25 4
gpt4 key购买 nike

我正在开发一个具有自定义布局和 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());
}
}

最佳答案

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

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

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

关于java - ListView Holder 检查复选框 Bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23706957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com