Return message if no checkbox is selected

Asked

Viewed 87 times

0

How to do the treatment in this code so that it returns message to the user select at least one checkbox if it 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());

}

2 answers

1


Another way would be to test the size of your list Listcheck(supposing that your list is a Collection) after passing through their if 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
  }

}

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
}
  • The treatment is working. But when I select more than one, only the first option comes when sending to BD.

  • I made an edit on my reply, go back to your code then, and at the end of everything, include a new if that will check whether your selected array is empty

Browser other questions tagged

You are not signed in. Login or sign up in order to post.