我已经设法检索活动中单击按钮后选中的复选框的数量,但是我希望能够更新活动中的文本视图以向用户显示所选项目的数量。
我该怎么做?
到目前为止,这是我的代码(在listadapter中扩展BaseAdapter):
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
答案 0 :(得分:1)
只需使用TextView功能:
TextView myTextView = (TextView) findViewById(R.id.MyTextViewId);
myTextView.setText(String.valueOf(myNumber));
你能告诉我们你的代码吗?你需要在这里施放
答案 1 :(得分:0)
使用以下代码
textview.setText( "" + count ):