带有 CheckBox 的 ListView 的奇怪行为

Weird Behaviour of ListView with CheckBox(带有 CheckBox 的 ListView 的奇怪行为)
本文介绍了带有 CheckBox 的 ListView 的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 ListView 上方有一个 CheckBox,其中包含 Checkbox 项.因此,当我检查复选框时,我应用 notifyDataSetChanged() 来检查所有检查列表的所有项目.所以当时我两次获取 getView 方法的日志,这意味着当我调用 notifyDataSetChanged() 一次时,getView 被调用了两次.那么,谁能告诉我为什么我会两次获取日志?还有为什么 getView() 会被调用两次?

I am having a CheckBox above the ListView having Checkbox items in it. So when I check the Checkbox I apply notifyDataSetChanged() to check all the check all the items of the List. So at that time I am getting logs for getView method two times, it means getView is getting called twice when I am calling notifyDataSetChanged() only once. So, can anyone tell me why I am getting logs twice? Also why getView() is called twice?

主类 -

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 输出何时我可以 adapter.notifyDataSetChanged(); 在 CheckAll 复选框上检查更改侦听器.

LogCat Output When I can adapter.notifyDataSetChanged(); on CheckAll checkboxs Check 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 输出两次得到相同的输出.那么有人能说出为什么会这样吗?

You can see that I the Logcat output I am getting the same output two times. So can anyone tell the why this is happening?

推荐答案

试着让你的 ListView 高度 fill_parent.

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

Try making your ListView height fill_parent.

Lisiview try itself to adjust a bit as per space provided, this is what I learned, and looks like this is the reason behind.

看看有没有帮助.

这篇关于带有 CheckBox 的 ListView 的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)