I'm trying to set a ListView that each row contains CheckBox, TextView and 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.
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:
@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.
move the following lines outside the if statement
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:selected.setChecked (checkedItems[position]);
The setSelected method sets the selection state of a row within a
ListView
(or anotherAdapterView
).
EDIT:
You set the setOnCheckedChangeListener
to the CheckBox
view. Then, you try to read the tag of the view that the setOnCheckedChangeListener
was set to -- this is the CheckBox
. However, you never set the tag of the CheckBox
! So, the tag you read in the setOnCheckedChangeListener
is wrong.
You have to set it like this:
holder.selected.setTag ("" + position);
... and then, in the setOnCheckedChangeListener
, read it like this:
checkedItems[Integer.parseInt (buttonView.getTag())] = isChecked;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.