Search code examples
javaandroidandroid-arrayadapter

How to restrict a user from selecting more than 10 items from a CheckBox ListView in Android


I've searched all over and cant find a solution that works for my situation. I currently have a custom checkbox adapter extending from ArrayAdapter that holds 'Food' objects ('Food' contains 2 attributes 1. Boolean:Selected and 2. String:Name). When the listView loads I have 10 items that are selected by default. I need to allow a user to unselect/select options but limit them to only 10 choices.

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


public class CheckBoxAdapter extends ArrayAdapter<Food> {

    Context context;
    ArrayList<Food> foodList;

    int counter = 0;

    private static class ViewHolder {
        TextView txtView;
        CheckBox chkBox;

        public ViewHolder(View view) {
            txtView = (TextView) view.findViewById(R.id.food_text);
            chkBox = (CheckBox) view.findViewById(R.id.food_box);
        }
    }

    public CheckBoxAdapter(Context context, ArrayList<Food> food) {
        super(context, 0, food);
        this.context = context;
        foodList = food;
    }

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

        Food food = getItem(position);

        View row = convertView;

        ViewHolder viewHolder = null;

        if (row == null) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.food_category_row, parent, false);
            viewHolder = new ViewHolder(row);
            row.setTag(viewHolder);

            //Not sure if we need to use this - for now i think its useless...
            viewHolder.chkBox.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.
                    //foodList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                    if(isChecked){
                        counter++;
                    }
                    else if(isChecked){
                        counter--;
                    }
                    if(counter > 10){
                        foodList.get(getPosition).setSelected(!buttonView.isChecked());
                        counter--;
                    }

                }
            });
        } else {
            viewHolder = (ViewHolder) row.getTag();

        }

        viewHolder.chkBox.setTag(position); // This line is important.
        viewHolder.txtView.setText(foodList.get(position).getName());
        viewHolder.chkBox.setChecked(foodList.get(position).isSelected());

        return row;

    }

}

Solution

  • You need to do something like that:

    viewHolder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (int) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                //foodList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                if (isChecked) {
                    counter++;
                } else {
                    counter--;
                }
                if (counter > 10) {
                    buttonView.setChecked(false); // no need to do counter-- because the listener will trigger again
                }
    
            }
        });