在单击活动中的按钮后,我设法检索了选中的复选框的数量,但是我希望能够更新活动中的文本视图以向用户显示所选项目的数量。
我该怎么做?
到目前为止,这是我的代码(在扩展 BaseAdapter 的 listadapter 中):
private ArrayList<Compare> selectedComparePlaces = new ArrayList<Compare>();
private int numSelected;
...
// in the getView method
TextView tv = (TextView) convertView.findViewById(R.id.compareSubheading);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.compareCheckbox);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked) {
numSelected++;
selectedComparePlaces.add(compareList.get(position));
// should update the textview to say "[numberselected]/2 selected"
// tv.setText(numSelected + "/2 selected");
} else {
numSelected--;
selectedComparePlaces.remove(compareList.get(position));
// should update the textview to say "[numberselected]/2 selected"
// tv.setText(numSelected + "/2 selected");
}
}
});
我试过使用: tv.setText(String.valueOf(numSelected) + "/2 selected");
但是这两次尝试都在 tv.setText 行上给了我一个 nullPointerException