【发布时间】:2015-12-07 14:00:23
【问题描述】:
我已经通过扩展 ArrayAdapter 类实现了一个自定义列表视图,我遇到的问题是,每当我滚动列表视图时,复选框项目都未选中,所以我实现了以下代码:
CheckBox cb = (CheckBox) myview.findViewById(R.id.grocery_item_checkbox);
//Code to keep the status of checkbox checked, when scrolling up or down
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(!compoundButton.isChecked()){
checkbox_status[pos] = false;
Log.d("LOG", "Checkbox at position" + pos + "is set to" + checkbox_status[pos]);
}else{
checkbox_status[pos] = true;
Log.d("LOG", "Checkbox at position" + pos + "is set to" + checkbox_status[pos]);
}
notifyDataSetChanged();
}
});
if(!checkbox_status[pos]){
cb.setChecked(false);
}else{
cb.setChecked(true);
}
这里checkbox_status 是一个布尔数组,用于存储复选框的状态。而pos指的是getView方法中的位置。
我遇到的问题是,每当我单击列表视图中的顶部项目时,底部的项目就会被检查:
假设我点击位置 0 的项目,但位置 6 的项目被选中。
当我向上或向下滚动时,复选框的状态也会再次受到干扰。
【问题讨论】: