how to limit checkbox selection in listview?

前端 未结 5 1262
暖寄归人
暖寄归人 2020-12-30 17:35

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.

u

5条回答
  •  温柔的废话
    2020-12-30 18:17

    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);
                }
            });
    

提交回复
热议问题