mercredi 15 juillet 2015

unpredictable behahviour of list view item's checkbox click

In a list view I am using checkboxes to select list list item, when checkbox is check then the background of the listview item should changed, but when I click checkbox of first item it's background changes but when I scrolls down list item 3's background and checkbox state change also. I don't get it why this happening may be due to listview's recycling process.

public View getView(final int position, View convertView, ViewGroup parent) {
        TextView tvTitle, tvCat, tvDate;
        CheckBox cbAddSummary, cbAddPhoto,cbItemSelect;
        Log.i("mponeList:position",String.valueOf(position));
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_mp_one,parent,false);
            tvTitle = (TextView)convertView.findViewById(R.id.tv_title);
            tvCat = (TextView) convertView.findViewById(R.id.tv_cat);
            tvDate = (TextView)convertView.findViewById(R.id.tv_date);
            cbAddSummary = (CheckBox) convertView.findViewById(R.id.cb_add_summary);
            cbAddPhoto = (CheckBox) convertView.findViewById(R.id.cb_add_photo);
            cbItemSelect = (CheckBox) convertView.findViewById(R.id.cb_item_select);
        }

        tvTitle = (TextView)convertView.findViewById(R.id.tv_title);
        tvCat = (TextView) convertView.findViewById(R.id.tv_cat);
        tvDate = (TextView)convertView.findViewById(R.id.tv_date);
        cbAddSummary = (CheckBox) convertView.findViewById(R.id.cb_add_summary);
        cbAddPhoto = (CheckBox) convertView.findViewById(R.id.cb_add_photo);
        cbItemSelect = (CheckBox) convertView.findViewById(R.id.cb_item_select);
        cbItemSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    View row = ((View) buttonView.getParent());
                    if (isChecked) {
                        row.setBackgroundResource(R.drawable.shape_mp_list_item_check);
                    } else {
                        row.setBackgroundResource(R.drawable.shape_mm_er_list);
                    }
                }
        });


        MpOneListModel model = this.list.get(position);
        tvTitle.setText(model.getTitle());
        tvCat.setText(model.getCat());
        tvDate.setText(model.getDate());

        return convertView;
    }

It's the code I am using in getview of the adapter.




Aucun commentaire:

Enregistrer un commentaire