ListView与CheckBox的奇怪行为

Lal*_*ani 6 android android-widget android-manifest android-emulator android-layout

我在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();
            }
        });
Run Code Online (Sandbox Code Playgroud)

适配器类代码 -

    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;
    }
}
Run Code Online (Sandbox Code Playgroud)

LogCat输出当我可以adapter.notifyDataSetChanged();在CheckAll上复选框时选中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
Run Code Online (Sandbox Code Playgroud)

你可以看到我在Logcat输出中得到两次相同的输出.那么有人能说出为什么会这样吗?

viv*_*viv 5

尝试制作你的ListView身高fill_parent.

Lisiview尝试按照所提供的空间调整一点,这是我学到的,看起来这就是背后的原因.

看看它是否有帮助.

  • 这意味着当你想将列表视图设置在屏幕的一半时..你无法解决上述问题? (2认同)