我有一个ListView,约有200个项目,我已经为复选框实现了自定义的ArrayAdapter。我使用SparseBooleanArray来存储框的值。
所有这些都很好用,但我似乎无法在图形上更新框的检查状态。如果用户单击,则框将被选中。但是,如果我在我的代码中调用setChecked,则它对框本身没有影响(因此,即使其值为true,它也不会被打勾)。
我尝试将所有框设置为true并没有成功,即使它们都被选中,它们仍然没有显示出来。不确定你需要看到什么代码,但我为了保险起见将我的适配器扔进来了:
所有这些都很好用,但我似乎无法在图形上更新框的检查状态。如果用户单击,则框将被选中。但是,如果我在我的代码中调用setChecked,则它对框本身没有影响(因此,即使其值为true,它也不会被打勾)。
我尝试将所有框设置为true并没有成功,即使它们都被选中,它们仍然没有显示出来。不确定你需要看到什么代码,但我为了保险起见将我的适配器扔进来了:
class CheckBoxArrayAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener {
private SparseBooleanArray checkedBoxes;
Context context;
public CheckBoxArrayAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
checkedBoxes = new SparseBooleanArray();
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final CheckBox view = (CheckBox) super.getView(position, convertView, parent);
//if(convertView == null) {
//convertView = view.inflate(context, R.layout.list_item, null);
//}
view.setTag(position);
view.setChecked(checkedBoxes.get(position, false));
System.out.println(view.getTag() + ": " + view.isChecked());
view.setOnCheckedChangeListener(this);
return view;
}
public boolean isChecked(int position) {
return checkedBoxes.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
checkedBoxes.put(position, isChecked);
notifyDataSetChanged();
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedBoxes.put((Integer) buttonView.getTag(), buttonView.isChecked());
}
感谢大家!