简体   繁体   English

android listview中的复选框有问题

[英]Checkboxes in android listview having problem

I have a listview with custom BaseAdapter and each row contains a checkbox and three textviews. 我有一个带有自定义BaseAdapter的列表视图,每行包含一个复选框和三个textview。 I am using Layoutinflater to inflate this row from a xml file. 我正在使用Layoutinflater从xml文件中扩充此行。 However, everytime I check one checkbox, many other checkboxes get checked in the whole list, whereas the original checkbox I wanted to check sometimes gets checked itself, and sometimes it does not. 但是,每次我选中一个复选框时,在整个列表中都会检查许多其他复选框,而我想检查的原始复选框有时会自行检查,有时则不会。

Every time a user selects a checkbox, I am storing that checkbox's attached unique value in a collection. 每次用户选中一个复选框时,我都会将该复选框附加的唯一值存储在集合中。 Next time the getView method is called, I manually check/uncheck the checkbox before returning the view inside getView() based on if the checkbox's value was already in the collection or not. 下次调用getView方法时,我会根据复选框的值是否已经在集合中,在getView()内部返回视图之前手动选中/取消选中该复选框。 But despite doing this, it is still marking off those checkboxes, even though the checkedchangelistener for those checkboxes is not firing up. 但尽管如此,它仍然标记了这些复选框,即使这些复选框的checkedchangelistener没有启动。 I am doubtful this due to views getting reused in getView, but dont know what is a good way to get this whole thing to work. 我很怀疑这是因为在getView中重复使用了视图,但是不知道什么是让这整个工作变得有效的好方法。

Avoid if (convertView==null) and else whole part It will definitely work for you. 避免if(convertView == null)和其他整个部分它肯定适合你。 Thanks. 谢谢。

the problem is definitely within your getView() method; 问题肯定在你的getView()方法中;

Try something like this 尝试这样的事情

public View getView(int position, View convertView, ViewGroup parent) {
        View vu = convertView;
        ViewHolder vHolder = null;
        try {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (vu == null) {
                vu = (View) inflater.inflate(R.layout.list_fr_req, null);
                vHolder = new ViewHolder();
                vHolder.checkbox = (CheckBox) vu.findViewById(R.id.my_ChkBox);
                vu.setTag(vHolder);
            } else {
                vHolder = (ViewHolder) vu.getTag();
            }
            vHolder.checkbox.setOnCheckedChangeListener(this);

            vHolder.checkbox.setId(position);
            vHolder.textView.setId(position);

            if (myList.get(position).getCheckedStatus())
                vHolder.checkbox.setChecked(true);
            else
                vHolder.checkbox.setChecked(false);

        } catch (Exception e) {
            Log.d("Exception in getview", e + "");
            e.printStackTrace();
        }
        return vu;
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            list.get(buttonView.getId()).setCheckedStatus(true);
        } else {
            list.get(buttonView.getId()).setCheckedStatus(false);
        }
    }

    public static class ViewHolder {
        CheckBox checkbox;
        TextView textview;
    }

Regards: N_JOY. 问候:N_JOY。

Use the OnClickListener instead of OnCheckedChangeListener. 使用OnClickListener而不是OnCheckedChangeListener。 The latter is triggered even when you update the recycled view to match the value of the object. 即使您更新循环视图以匹配对象的值,也会触发后者。 In the following piece of code of a getView() I'm using an HashMap to store the check value of the objects (itemMap). 在下面的getView()代码中,我使用HashMap来存储对象的检查值(itemMap)。

            boolean checked=itemMap.get(currentObject);
            if(checked!=checkBox.isChecked())
                checkBox.setChecked(checked);
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox checkBox1=(CheckBox)v;
                    if(checkBox1.isChecked()) //ckecked
                    {
                        itemMap.put(currentObject,true);
                        if(checkListener!=null)
                            checkListener.onCheck(position,convertView,parent,adapter);
                    }
                    else{ //unchecked
                        itemMap.put(currentObject,false);
                        if(checkListener!=null)
                            checkListener.onUnCheck(position,convertView,parent,adapter);
                    }
                }
            });

checkListener is an additional listener that can be added by the user of the class. checkListener是一个额外的侦听器,可以由类的用户添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM