Skip to content Skip to sidebar Skip to footer

Onscroll Selected Checkbox Getting Unchecked Using Listiview

In my application i added checkbox with ListView,and i also gave limitation for checkbox,user can not select more than 5 checkbox,but the issue is on scroll my selected checkbox ge

Solution 1:

Problem is in your getView() method. You set OnCheckedChangeListener and after that you set checkbox to true or false which fires callback in that listener. You should set checkbox check state first and after that set OnCheckedChangeListener.

Also, that boolean checked[] field is useless, so I simplified your code a little:

publicclassCustomAdapterextendsBaseAdapter {
    privatefinal LayoutInflater inflater;
    privatefinal Context context;
    private List<ModelPooja> listData;

    publicCustomAdapter(Context mainActivity, List<ModelPooja> listData) {
        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @OverridepublicintgetCount() {
        return listData.size();
    }

    @Overridepublic Object getItem(int position) {
        return listData.get(position);
    }

    @OverridepubliclonggetItemId(int position) {
        return0;
    }

    @Overridepublic View getView(finalint position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = newViewHolder();
            convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        if (listData.get(position).isselected) {
            holder.checks.setChecked(true);
        } else {
            holder.checks.setChecked(false);
        }

        holder.checks.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
            @OverridepublicvoidonCheckedChanged(CompoundButton cb, boolean b) {

                if (checkMaxLimit()) {

                    if (listData.get(position).isselected && b) {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                    } else {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (b) {
                        listData.get(position).isselected = true;
                    } else {
                        listData.get(position).isselected = false;
                    }
                }
            }
        });

        holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
        return convertView;
    }

    publicbooleancheckMaxLimit() {
        intcountermax=0;
        for(ModelPooja item : listData){
            if(item.isselected){
                countermax++;
            }
        }
        return countermax >= 5;
    }

    publicclassViewHolder {
        TextView tv;
        public CheckBox checks;
    }
}

Solution 2:

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

publicclassAddressModel {

    privateboolean isChecked;
    private String address;
    privateint alAddressDelete;

    publicAddressModel(){

    }
    publicAddressModel(boolean isChecked, String address, int alAddressDelete) {
        this.isChecked = isChecked;
        this.address = address;
        this.alAddressDelete = alAddressDelete;
    }

    publicbooleangetIsChecked() {
        return isChecked;
    }

    publicvoidsetIsChecked(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.

Solution 3:

Call setOnCheckedChangeListener() first, then call setChecked(), otherwise the setChecked() will trigger the previous ViewHolder object's setOnCheckedChangeListener().

Post a Comment for "Onscroll Selected Checkbox Getting Unchecked Using Listiview"