[英]Checkboxes in ListView mixed when scrolling the list
I'm trying to set a ListView that each row contains CheckBox, TextView and ImageView. 我试图设置一个ListView,每行包含CheckBox,TextView和ImageView。 I use my own adapter in the implementation, but when I'm scrolling down some Checkbox is checked without no reason: For example, I check checkbox in the first row (in the 0 position), scrolling down and suddenly the element in row 17 is also checked, and sometime if I turned back to the first the checkbox is not checked.
我在实现中使用了自己的适配器,但是当我向下滚动时,没有任何理由地选中了Checkbox:例如,我在第一行(位于0位置)选中了Checkbox,向下滚动并突然在第17行中的元素也被选中,有时如果我回到第一个复选框,则不会被选中。
I used the following code: 我使用以下代码:
private class ViewHolder{
ImageView image;
TextView name;
CheckBox selected;
}
private class BrowserAdapter extends ArrayAdapter<String> {
public BrowserAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.browser_list_row, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) convertView.findViewById(R.id.browser_image);
holder.name = (TextView) convertView.findViewById(R.id.browser_file_name);
holder.name.setOnClickListener(ActionBar.this);
holder.selected = (CheckBox) convertView.findViewById(R.id.browser_selected);
holder.selected.setOnCheckedChangeListener(ActionBar.this);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
setViewInfo(holder.image, holder.name, holder.selected, position);
return convertView;
}
private void setViewInfo(ImageView image, TextView name, CheckBox selected , int position) {
name.setTag(position);
selected.setTag(position);
selected.setChecked(checkedItems[position]);
name.setText(myList.get(position));
File temp_file = new File( file, myList.get( position ) );
if (temp_file.isFile())
image.setImageResource(R.drawable.mp3file);
else
image.setImageResource(R.drawable.folder);
}
}//class BroswerAdapter
The argument checkedItems is a boolean array that save the state of the checkboxes in the list like in the following code: 参数checkedItems是一个布尔数组,将复选框的状态保存在列表中,如以下代码所示:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedItems[(Integer)buttonView.getTag()] = isChecked;
}
Can you please help me to figure out what I'm doing wrong? 您能帮我弄清楚我在做什么错吗?
Edit: when I use the naive way of inflating a view every time the getView function is called (without using the convertView) everything works good. 编辑:当我每次调用getView函数(不使用convertView)时都使用膨胀视图的天真方法时,一切正常。
move the following lines outside the if statement 将以下行移至if语句之外
holder.name.setOnClickListener(ActionBar.this);
holder.selected = (CheckBox) convertView.findViewById(R.id.browser_selected);
holder.selected.setOnCheckedChangeListener(ActionBar.this);
WRONG: 错误:
You are using
setSelected
, while you should be using setChecked on theCheckBox
View like this:您正在使用
setSelected
,而应在CheckBox
视图上使用setChecked ,如下所示:selected.setChecked (checkedItems[position]);
The setSelected method sets the selection state of a row within a
ListView
(or anotherAdapterView
).setSelected方法设置
ListView
(或另一个AdapterView
) 中行的选择状态。
EDIT: 编辑:
You set the setOnCheckedChangeListener
to the CheckBox
view. 您将
setOnCheckedChangeListener
设置为CheckBox
视图。 Then, you try to read the tag of the view that the setOnCheckedChangeListener
was set to -- this is the CheckBox
. 然后,您尝试读取
setOnCheckedChangeListener
设置为的视图的标记-这是CheckBox
。 However, you never set the tag of the CheckBox
! 但是,您永远不会设置
CheckBox
的标签! So, the tag you read in the setOnCheckedChangeListener
is wrong. 因此,您在
setOnCheckedChangeListener
读取的标签是错误的。
You have to set it like this: 您必须这样设置:
holder.selected.setTag ("" + position);
... and then, in the setOnCheckedChangeListener
, read it like this: ...然后在
setOnCheckedChangeListener
,如下所示:
checkedItems[Integer.parseInt (buttonView.getTag())] = isChecked;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.