[英]CheckBox gets unchecked on scroll in a custom listview, didn't find a solution
I know this question has a lot of answers in the forum, but I tried all answers and none of them works with my code. 我知道这个问题在论坛上有很多答案,但是我尝试了所有答案,但没有一个能与我的代码一起使用。 The checkbox is unchecked whenever the list is scrolled down.
每当列表向下滚动时,该复选框均未选中。 Here is my code:
这是我的代码:
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;
}
} }
Please anyone can help me? 请任何人可以帮助我吗?
Have you tired using an OnCheckedChangeListener instead of an OnClickListener
? 您是否对使用OnCheckedChangeListener而不是
OnClickListener
感到厌倦? You can then just assign whatever value you get from isChecked
into your checkBoxState
array. 然后,您可以将从
isChecked
获得的任何值分配给checkBoxState
数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.