Return message if no checkbox is selected

0

How do I make the treatment in this code so that it returns a message to the user to select at least one checkbox if he does not select any?

    public void verificaCheckBox(){

    Listcheck.clear();

    if (cbPapel.isChecked())
        Listcheck.add(cbPapel.getText().toString());

    if (cbPlastico.isChecked())
        Listcheck.add(cbPlastico.getText().toString());

    if (cbMetal.isChecked())
        Listcheck.add(cbMetal.getText().toString());

    if (cbVidro.isChecked())
        Listcheck.add(cbVidro.getText().toString());


        cbSelecionado = (Listcheck.toString());

}
    
asked by anonymous 20.04.2018 / 17:02

2 answers

1

Another way would be to test the size of your Listcheck list (assuming that your list is a Collection ) after passing its if of addition.

public void verificaCheckBox(){

  Listcheck.clear();

  if (cbPapel.isChecked())
    Listcheck.add(cbPapel.getText().toString());

  if (cbPlastico.isChecked())
    Listcheck.add(cbPlastico.getText().toString());

  if (cbMetal.isChecked())
    Listcheck.add(cbMetal.getText().toString());

  if (cbVidro.isChecked())
    Listcheck.add(cbVidro.getText().toString());

  if(Listcheck.size() > 0){
    cbSelecionado = (Listcheck.toString());
  }else{
    //nenhum checkbox selecionado
  }

}

    
20.04.2018 / 17:18
0
if (cbPapel.isChecked())
   Listcheck.add(cbPapel.getText().toString());

if (cbPlastico.isChecked())
   Listcheck.add(cbPlastico.getText().toString());

if (cbMetal.isChecked())
   Listcheck.add(cbMetal.getText().toString());

if (cbVidro.isChecked())
   Listcheck.add(cbVidro.getText().toString());

if (Listcheck.isEmpty()){
   //Msg que não foi selecionado nenhum
}
    
20.04.2018 / 17:10