Android_Superman Android_Superman - 4 months ago 19
Android Question

Limit checks in checkboxes in listview to 3

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
planet.setChecked(isChecked);
if(isChecked)
{
counter=counter+1;
Toast.makeText(getContext(),String.valueOf(counter),Toast.LENGTH_LONG).show();


}
else
{
if(counter>3)
{
counter=3;
}
counter=counter-1;
Toast.makeText(getContext(),String.valueOf(counter),Toast.LENGTH_LONG).show();


}
if(counter>3)
{
checkBox.setChecked(false);

}

}
});


I have used the above code in
getView
method Its working fine in a list but when the list scrolls
onCheckedChanged
method is called

Answer

Try this adapter it will work for you

public class CustomAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private final Context context;
    private List<MyModelClass> listData;

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

    @Override
    public int getCount() {
        return listData.size();
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_name);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_check);
            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(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(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).getLISTING_NAME());
        return convertView;
    }

    public boolean checkMaxLimit() {
        int countermax = 0;
        for(MyModelClass item : listData){
            if(item.isselected){
                countermax++;
            }
        }
        return countermax >= 3;
    }

    public class ViewHolder {
        TextView tv;
        public CheckBox checks;
    }
}