我知道这个问题在论坛中有很多答案,但我尝试了所有答案,但没有一个适用于我的代码。每当列表向下滚动时,该复选框都不会被选中。这是我的代码:
public class ContactsAdapter extends ArrayAdapter<User> {
private LayoutInflater inflater;
boolean[] checkBoxState;
ViewHolder holder;
public ContactsAdapter(Context context, ArrayList<User> list) {
super(context, R.layout.listitemlayout, R.id.itemTextView, list);
inflater = LayoutInflater.from(context);
checkBoxState = new boolean [list.size()];
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listitemlayout, null);
holder.itemTextView = (TextView)convertView.findViewById(R.id.itemTextView);
holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
User user = getItem(position);
holder.itemTextView.setText(user.getName());
holder.checkbox.setTag(user);
holder.checkbox.setChecked(checkBoxState[position]);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked())
checkBoxState[position]=true;
else
checkBoxState[position]=false;
}
});
return convertView;
}
private class ViewHolder {
TextView itemTextView;
CheckBox checkbox;
}
}
请问有人可以帮助我吗?