title: How to determine the number of checkboxes that can be checked in Android?
body: <p>hi I have checkboxes inside a listview 
  I have eight choices, and I want the user to be able to check only four, then lock the checkboxes unless he unchecks one. </p>
  
  <p>This my Main:</p>
  
  <pre><code>public class Ch2 extends Activity implements OnItemClickListener{
      ListView lv;
      ArrayAdapter&lt;Model&gt; adapter;
      List&lt;Model&gt; list = new ArrayList&lt;Model&gt;();
      int num;
  
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          setContentView(R.layout.activity_ch2);
  
          lv = (ListView) findViewById(R.id.listView2);
          adapter = new cusCh2(this,getModel());
          lv.setAdapter(adapter);
          lv.setOnItemClickListener(this);
      }
  
      @Override
      public void onItemClick(AdapterView&lt;?&gt; arg0, View v, int position, long arg3) {
          TextView label = (TextView) v.getTag(R.id.label);
          final CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
          checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
  
              @Override
              public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                  if (isChecked){
  
                      Toast.makeText(getApplicationContext(),"hi",Toast.LENGTH_LONG);
  
                       // disable checkbox
                  }
              }
          });
          Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
  
      }
  
      private String isCheckedOrNot(CheckBox checkbox) {
          if(checkbox.isChecked()) {
              num++;
              if (num == 4) {
                  checkbox.setEnabled(false);
  
              }
              return "is checked";
  
          } else
              return "is not checked";
      }
  
  
      private List&lt;Model&gt; getModel() {
          list.add(new Model("a"));
          list.add(new Model("b"));
          list.add(new Model("c"));
          list.add(new Model("d"));
          list.add(new Model("e"));
          list.add(new Model("f"));
          list.add(new Model("m"));
          list.add(new Model("o"));
          return list;
      }
  </code></pre>
  
  <p>and this my ArrayAdapter</p>
  
  <pre><code>public class cusCh2  extends ArrayAdapter&lt;Model&gt; {
  
      private final List&lt;Model&gt; list;
      private final Activity context;
      boolean checkAll_flag = false;
      boolean checkItem_flag = false;
  
  
      public cusCh2(Activity context, List&lt;Model&gt; list) {
          super(context, R.layout.row, list);
          this.context = context;
          this.list = list;
      }
  
      static class ViewHolder {
          protected TextView text;
          protected CheckBox checkbox;
  
  
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
  
  
  
      ViewHolder viewHolder = null;
      if (convertView == null) {
          LayoutInflater inflator = context.getLayoutInflater();
          convertView = inflator.inflate(R.layout.row, null);
          viewHolder = new ViewHolder();
          viewHolder.text = (TextView) convertView.findViewById(R.id.label);
          viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
          final ViewHolder finalViewHolder = viewHolder;
          final ViewHolder finalViewHolder1 = viewHolder;
          final ViewHolder finalViewHolder2 = viewHolder;
          viewHolder.checkbox.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.
                  list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
  
              }
          });
          convertView.setTag(viewHolder);
          convertView.setTag(R.id.label, viewHolder.text);
          convertView.setTag(R.id.check, viewHolder.checkbox);
      } else {
          viewHolder = (ViewHolder) convertView.getTag();
      }
      viewHolder.checkbox.setTag(position); // This line is important.
  
      viewHolder.text.setText(list.get(position).getName());
      viewHolder.checkbox.setChecked(list.get(position).isSelected());
  
      return convertView;
  }
  }
  </code></pre>
  
  <p>and this my model</p>
  
  <pre><code>public class Model {
  
      private String name;
      private boolean selected;
  
      public Model(String name) {
          this.name = name;
      }
  
      public String getName() {
          return name;
      }
  
      public boolean isSelected() {
          return selected;
      }
  
      public void setSelected(boolean selected) {
          this.selected = selected;
      }
  </code></pre>
  
  <p>of course I have <code>row.xml</code> file </p>
  
  <p>as I say, I want to make my user able to check at most four boxes.</p>
  
  <p>The solution I tried:</p>
  
  <pre><code>    @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.
          list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
          if(!isChecked){
              num++;
  
              if(num&gt;3){
                 finalViewHolder.checkbox.setChecked(false);}
              if(!isChecked){
                  num--;
                  if(num&lt;3){
                      finalViewHolder.checkbox.setChecked(true);}
              }
  
  
          }
      }
  });
  </code></pre>
  
  <p>It's working as stop check, but when I scroll list, I lose what I already checked.</p>
  
  <p>edit 2:</p>
  
  <pre><code>      viewHolder.checkbox.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.
                list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
  
                 Toast toast=  Toast.makeText(getContext(), String.valueOf(getPosition), Toast.LENGTH_SHORT);
  
                  if (isChecked) {
                      num++;
                      l.add(String.valueOf(getPosition));
                      if(l.size() &gt; 3){    
                          finalViewHolder.checkbox.setChecked(false);
                      }         
                      toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
                      toast.show();
                  }
  
                      else if(!isChecked){
                      finalViewHolder.checkbox.setChecked(true);
                      }
  
              }
  
          });
  </code></pre>
  
  <p>also when i scroll i loss what i already checked </p>
  
  <p>edit 2:</p>
  
  <pre><code>   viewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
  
                  int getPosition = (Integer) v.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                  list.get(getPosition).setSelected(finalViewHolder.checkbox.isChecked()); // Set the value of checkbox to maintain its state.
                  Toast toast=  Toast.makeText(getContext(), String.valueOf(getPosition), Toast.LENGTH_SHORT);
  
                  if (finalViewHolder.checkbox.isChecked()) {
  
                      l.add(String.valueOf(getPosition));
                      toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                      toast.show();
  
                      if (l.size() &gt; 4) {
                          finalViewHolder.checkbox.setChecked(false);
                      }
  
  
                      if (!finalViewHolder.checkbox.isChecked()) {
                          finalViewHolder.checkbox.setChecked(true);
  
                      }
  
                  }
              }
  
          }
      );
  </code></pre>
  
  <p>I hope someone can help me, thanks.</p>