更新#2 我有一个listView和一个适配器。 listView中的每个项目都有一个复选框。 单击列表视图中的复选框时,下面的5-6项复选项(重复 - 所有复选框)也显示为按下 - 灯亮,即使没有人点击,然后检查是否实际按下了'isChecked'bool flag,这是假的。
public class watchAllAdapter扩展了ArrayAdapter {
public watchAllAdapter(Context context, ArrayList<Subject> arrayList) {
super(context, 0, arrayList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.for_each_watch_all, parent, false);
}
final Subject currSubject = getItem(position);
final CheckBox cb = (CheckBox) listItemView.findViewById(R.id.checkbox_for_each);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked){
currSubject.setChosen(true);
}
else{
cb.setChecked(false);
currSubject.setChosen(false);
}
}
});
return listItemView;
}
thanks =]
答案 0 :(得分:0)
如果我是你,我建议你使用......
if(cb.isChecked()) {
currSubject.setChosen(true);
}
else if (!cb.isChecked()) {
currSubject.setChosen(false);
}
答案 1 :(得分:0)
尝试遵循View Holder模式。创建一个Holder类,如果它不为null,则重用该视图。如果视图已经存在,也将onCheckedChangedListener()设置为复选框。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
Holder holder = null;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.for_each_watch_all, parent, false);
holder = new Holder();
holder.ckbox =(CheckBox)row.findViewById(R.id.check_box_id);
listItemView .setTag(holder);
}else {
holder = (Holder) listItemView.getTag();
holder.ckbox.setOnCheckedChangeListener(null);
}
final Subject currSubject = getItem(position);
final CheckBox cb = (CheckBox) listItemView.findViewById(R.id.checkbox_for_each);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked){
currSubject.setChosen(true);
}
else{
cb.setChecked(false);
currSubject.setChosen(false);
}
}
});
return listItemView;
}
private class ViewHolder {
CheckBox checkBox;
}