该问题有两种解决方案,个人目前所实现了的。
1.用HashMap保存checkbox的状态值。
HashMap<Integer, Boolean> state = new HashMap<Integer,Boolean>();
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked) {
state.put(position, isChecked);
System.out.println("复选框以选中,选中的行数为:" + temp_position);
}else{
state.remove(position);
}
}
在getView()方法里面: holder.cbox.setChecked(state.get(position)==null? false : true);
2.(不推荐使用,因为会产生许多垃圾对象)
public View getView(int position, View convertView, ViewGroup parent)在每次传进convertView时候,设为null。
然后每调用一次getView就产生一个view对象。
本文详细介绍了两种实现复选框状态保存与获取的方法:一种使用HashMap,另一种则产生多个垃圾对象。重点阐述了HashMap的使用场景及在getView方法中的应用,同时讨论了内存管理和性能优化。
2362

被折叠的 条评论
为什么被折叠?



