When scrolling custom ListView, the checkbox value changes

前端 未结 8 1945
不思量自难忘°
不思量自难忘° 2020-12-05 07:30


What I have: a custom listview with Textviews and checkbox.


Problem: Suppose my screen can show only 6 items of list at time, and
8条回答
  •  既然无缘
    2020-12-05 07:59

    You are setting the tag of the checkbox only if convertview is null. This happens only for the first screen of records. When user scrolls down, the previous convertviews are recycled. Thus your checkboxes have older data items as their tags.

    Your checked change listener should look like this:

    new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
        {
            Person element = (Person) viewHolder.checkBox.getTag();
    
            data[position].setCheck(isChecked);
    
            if(isChecked)
            {
                // do your stuff
            }
            else
            {
                //to-do
            }
    
        }
    }
    

提交回复
热议问题