我有一个列表视图,每行都有一个复选框。我正在使用 viewholder 模式来加载列表,如下所示:
View view = convertView;
if(view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.setting_list_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.cbCat = (CheckBox) view.findViewById(R.id.cbCat);
view.setTag(viewHolder);
}
ViewHolder vh = (ViewHolder) view.getTag();
vh.cbCat.setText(bt.getName());
但是当我尝试获取选中的复选框时,如下所示:
for(int i = lvCatList.getFirstVisiblePosition(); i < settingAdapter.getCount(); i++) {
View v = lvCatList.getChildAt(i);
CheckBox cbx = (CheckBox) v.findViewById(R.id.cbCat);
if(cbx != null && cbx.isChecked()) {
toast += " / " + settingAdapter.getItem(i).getName();
Log.d("CHECKED", toast);
}
只能获取可见的复选框。如果复选框在屏幕中不可见,则会引发异常。
如何检查列表中的复选框是否被选中。?
}