Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to limit checkbox selection in listview?

friends,

i want to limit checkbox selection in android listivew to for example only 3 checkboxes should be selected otherwise it should give error message.

user can select any three checkboxes from the list any one guide me how to achieve this? here is my adapter

public class AdapterContacts extends BaseAdapter  {

    private LayoutInflater mInflater;

    public Context context;
    public static List<myContacts> contacts;

    public AdapterContacts(Context context,List<myContacts> list) {
      mInflater = LayoutInflater.from(context);
      this.context = context;
      contacts= list;
    }

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

      ViewHolder holder;

      if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_contacts, null);
        holder = new ViewHolder();
        holder.contactName = (TextView) convertView.findViewById(R.id.contactName);
        holder.contactNumber = (TextView) convertView.findViewById(R.id.contactNumber);
        holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
        convertView.setTag(holder);

      } else {
        holder = (ViewHolder) convertView.getTag();
      }

      myContacts contact = getItem(position);
      holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) {
                 myContacts c = (myContacts) checkboxView.getTag();
                 c.setSelected(isChecked);
                 // to put that check of selection limit with error


            }
        });


      holder.checkBox.setTag(contact);
      holder.checkBox.setChecked(contact.isSelected());



      holder.contactName.setText(contact.getContactName());
      holder.contactNumber.setText(contact.getPhoneNumber());

        return convertView;

        }

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

    @Override
    public myContacts getItem(int position) {
        return contacts.get(position);
    }

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


    class ViewHolder {
        TextView contactName;
        TextView contactNumber;
        CheckBox  checkBox;
      }




}

any help would be appreciated.

like image 651
UMAR-MOBITSOLUTIONS Avatar asked Dec 22 '22 13:12

UMAR-MOBITSOLUTIONS


1 Answers

finally i solved this issue :) where as globalInc is global variable with default value 0

holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() 
      {

            @Override
            public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) 
            {
                 myContacts c = (myContacts) checkboxView.getTag();

                 if(isChecked)
                 {
                     globalInc++;
                 }
                 else if(!isChecked)
                 {
                     globalInc--;
                 }
                 if(globalInc >= 4)// it will allow 3 checkboxes only
                 {
                     Toast.makeText(context, "Error = " + globalInc, Toast.LENGTH_LONG).show();
                     checkboxView.setChecked(false);
                     globalInc--;
                 }
                 else
                 {
                    c.setSelected(isChecked);
                 }
                 System.out.println(" ---------------    "+globalInc);
            }
        });
like image 148
UMAR-MOBITSOLUTIONS Avatar answered Jan 07 '23 03:01

UMAR-MOBITSOLUTIONS