Onscroll selected checkbox getting unchecked using listiview

后端 未结 4 1684
日久生厌
日久生厌 2020-12-02 03:08

In my application i added checkbox with ListView,and i also gave limitation for checkbox,user can not select more than 5 checkbo

4条回答
  •  情深已故
    2020-12-02 03:55

    This is because when you scroll your listview from upper or lower side of phone, all items of listview are regenerated and they are set to default value. To avoid this make a model class which holds the current value of checkbox(true or false) and set the value of checkbox in adapter by calling getter method from that model instance. For example:

    model.java

    public class AddressModel {
    
        private boolean isChecked;
        private String address;
        private int alAddressDelete;
    
        public AddressModel(){
    
        }
        public AddressModel(boolean isChecked, String address, int alAddressDelete) {
            this.isChecked = isChecked;
            this.address = address;
            this.alAddressDelete = alAddressDelete;
        }
    
        public boolean getIsChecked() {
            return isChecked;
        }
    
        public void setIsChecked(boolean isChecked) {
            this.isChecked = isChecked;
        }
    }
    

    In your Adapter class set the value of checkbox by calling setIsChecked(boolean isChecked) in the setOnCheckedChangeListener(). call getIsChecked() where you are setting value of your checkboxes.

提交回复
热议问题