gpt4 book ai didi

android - 带有 CheckBox 的 ListView 的奇怪行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:48:25 25 4
gpt4 key购买 nike

我在 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;
}
}

当我可以 adapter.notifyDataSetChanged(); 在 CheckAll 复选框上检查 Change Listener 时,LogCat 输出。

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 输出两次得到相同的输出。那么谁能说出为什么会这样?

最佳答案

尝试使您的 ListView 高度为 fill_parent

Lisiview 尝试自己根据提供的空间进行一些调整,这是我学到的,看起来这就是背后的原因。

看看是否有帮助。

关于android - 带有 CheckBox 的 ListView 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8340401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com