2

I have a listview with custom BaseAdapter and each row contains a checkbox and three textviews. I am using Layoutinflater to inflate this row from a xml file. 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. But despite doing this, it is still marking off those checkboxes, even though the checkedchangelistener for those checkboxes is not firing up. 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.

elto
  • 447
  • 2
  • 9
  • 13

3 Answers3

6

Avoid if (convertView==null) and else whole part It will definitely work for you. Thanks.

SALMAN
  • 2,031
  • 1
  • 20
  • 18
3

the problem is definitely within your getView() method;

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.

Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
N-JOY
  • 10,344
  • 7
  • 51
  • 69
  • I'm not sure but when you call vHolder.checkbox.setChecked(false); (in listener), the onCheckedChangeListener isn't called again by the system? – Ungureanu Liviu Jan 10 '11 at 09:22
  • 2
    hey buddy this is my code and it is working fine. this was not question , i posted answer. – N-JOY Jan 10 '11 at 09:30
  • in some Android phone, setChecked() will also trigger onCheckedChanged() to be called, which will result unexpected result. – shihpeng Sep 04 '12 at 04:08
0

Use the OnClickListener instead of 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).

            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.