ListView控件的使用复选框怪异行为控件、怪异、复选框、行为

2023-09-13 01:54:11 作者:点背不能怨社会

我有有它复选框项目的ListView上面的复选框。所以,当我检查我申请的复选框 notifyDataSetChanged()检查所有的检查清单中的所有项目。所以当时我正在日志getView方法的两倍,这意味着getView获取调用两次,当我打电话 notifyDataSetChanged()只有一次。那么,谁能告诉我为什么我收到日志两次?也因如此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();
            }
        });

$ C $下适配器类 -

Code for Adapter Class -

    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 checkboxs检查更改监听器

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.

看看是否有帮助。