In Custom List view check box is unchecked when I scrolling

醉酒当歌 提交于 2019-12-02 12:24:53

Your getView method should look like this

Create a ViewHolder class

static class ViewHolder {
        TextView datetime;
        TextView totminutes;
        TextView skills;
        TextView weather;
        CheckBox chkdelete;
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.logitem1, null);
        viewHolder = new ViewHolder();
        viewHolder.datetime = (TextView) convertView.findViewById(R.id.id_datetime);
        // do the same thing for other textviews and checkbox.
        viewHolder.chkdelete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.

            }
        });
        convertView.setTag(viewHolder);
        convertView.setTag(R.id.id_datetime, viewHolder.datetime);
        // do the same for other textviews and checkbox
        } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.chkdelete.setTag(position); // This line is important.

    viewHolder.datetime.setText(resultp.get("Skill_practice"));
    // do the same for others

    return convertView;
}

You need to modify it according to your needs. This code will solve your problem on scroll.

You need to use for example a sparse boolean array. https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

Listview are recycled so you basically need to save the state of your checkbox somewhere.

Juste create a sparse boolean array with a size [number of checkbox].

When one of your checkbox is clicked, get his position and set in the sparse boolean array the element (at this position) to true.

In the create view just set the checkbox to the state "enabled" IF your sparse boolean is true.

And you should use a view holder instead of creating new object each time.

Good luck !

The buttonView in

onCheckedChanged(CompoundButton buttonView, boolean isChecked) 

refers to the last item of the adapter. This is because adapter is used to recycle view. To access the id , store it in the tag.

chkdelete.setTag(R.id.id_chkDelete);

Finally you can access the id of the view via its tag inside onCheckedChanged() as

int checkBoxId=(int) view.getTag().toString();

Finally, your code looks like

    SelectedBox = new ArrayList<Integer>();
    chkdelete.setTag(R.id.id_chkDelete);
    chkdelete.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
        {
            int checkBoxId=(int) view.getTag().toString();
            if(SelectedBox.size()-1==0)
            {
                menu.setVisible(false);
                addlog.setVisible(true);
            }else
            {
                addlog.setVisible(false);
            }
            if(isChecked)
            {
                SelectedBox.add(checkBoxId);
                menu.setVisible(true);
                addlog.setVisible(false);
            }else if(!isChecked)
            {
                SelectedBox.remove(SelectedBox.indexOf(checkBoxId));
            }

        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!