我在ListView上面有一个CheckBox,里面有Checkbox项目。因此,当我选中复选框时,我应用notifyDataSetChanged()
检查所有检查列表中的所有项目。所以当时我正在为getView方法获取两次日志,这意味着当我只调用notifyDataSetChanged()
一次时,getView会被调用两次。那么,谁能告诉我为什么我要两次获取日志?为什么getView()被调用两次?
主类 -
checkAll = (CheckBox) findViewById(R.id.my_check_box);
checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkbox, boolean arg1) {
adapter.notifyDataSetChanged();
}
});
适配器类代码 -
public class myAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
private CheckBox checkAll;
public myAdapter(Activity context, List<Model> list, CheckBox checkAll) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
this.checkAll = checkAll;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
if(checkAll.isChecked() == true){
System.out.println(position+" checked th position");
}
else if(checkAll.isChecked() == false){
System.out.println(position+" not checked th position");
}
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
LogCat输出当我可以在CheckAll上adapter.notifyDataSetChanged();
复选框时选中Change Listener。
11-30 20:31:33.751: INFO/System.out(1300): 0 checked th position
11-30 20:31:33.761: INFO/System.out(1300): 1 checked th position
11-30 20:31:33.770: INFO/System.out(1300): 2 checked th position
11-30 20:31:33.780: INFO/System.out(1300): 3 checked th position
11-30 20:31:33.810: INFO/System.out(1300): 4 checked th position
11-30 20:31:33.810: INFO/System.out(1300): 0 checked th position
11-30 20:31:33.831: INFO/System.out(1300): 1 checked th position
11-30 20:31:33.831: INFO/System.out(1300): 2 checked th position
11-30 20:31:33.851: INFO/System.out(1300): 3 checked th position
11-30 20:31:33.860: INFO/System.out(1300): 4 checked th position
你可以看到我的Logcat输出我得到两次相同的输出。那么有人能说出这种情况发生的原因吗?
答案 0 :(得分:5)
尝试制作ListView
身高fill_parent
。
Lisiview尝试根据提供的空间进行调整,这是我学到的,看起来这就是背后的原因。
看看它是否有帮助。
答案 1 :(得分:3)
这不是你问题的真正答案 - 只是当我看到它时,它会伤害我的眼睛:
if(checkAll.isChecked() == true){
System.out.println(position+" checked th position");
}
else if(checkAll.isChecked() == false){
System.out.println(position+" not checked th position");
}
if-statement
需要boolean
表达式,因为方法isChecked()
会返回boolean
,所以无需将其与true/false
匹配。此外,由于boolean
只能是true
或false
,因此无需检查两者。简而言之,你可以将上面的内容归结为:
if( checkAll.isChecked() )
System.out.println(position+" checked th position");
else
System.out.println(position+" not checked th position");
哪个更漂亮: - )
不要将此视为对编程技巧的批评 - 只是将其视为一种学习机会。
答案 2 :(得分:0)
这是因为checkchangelistener中的viewHolder会随着列表滚动而改变。因此,在复选框和viewHolder之间没有映射可以使用这种方式。
创建sepatera类checkchangelistener实现checnChecnglistener。通过构造函数传递checkchangelistener。
<强> EG。在getView外面
class MyCheckChangeLsitenet implements CompoundButton.OnCheckedChangeListener
ViewHolder viewHolder
MyCheckChangeLsitenet(ViewHolder viewHolder)
{
this.viewHolder = viewHolder ;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
GetView中的
viewHolder.checkbox .setOnCheckedChangeListener(new MycheckChangeListenet(viewHolder))