Checkboxes in Alertdialog always return the same values

Asked

Viewed 78 times

4

I have a AlertDialog with a custom layout with 2 checkboxes. But even if I click one of these checkboxes, by clicking the accept or cancel buttons (alert.setPositiveButton or the alert.setNegativeButton), I get the default values of checkboxes and I don’t know if I clicked on them or not.

Creation of the Alertdialog

AlertDialog.Builder alert = new AlertDialog.Builder(novo_layout.this);
alert.setCancelable(false);
LayoutInflater inflater = this.getLayoutInflater();
alert.setView(inflater.inflate(R.layout.layout_alert, null));
alert.setMessage(IdAsString);

Variables from the checkboxes

View view = inflater.inflate(R.layout.layout_alert, null);
sentado = (CheckBox)view.findViewById(R.id.sentado);
livre = (CheckBox)view.findViewById(R.id.livre);

Checking of checkboxes

alert.setPositiveButton("X", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {

    if(!sentado.isChecked()&&!livre.isChecked()){
         Toast.makeText(getApplicationContext(), "Os dados não foram gravados, indroduza uma posição", Toast.LENGTH_SHORT).show();
     //checkBoxClicked.setChecked(false);
    }

Every time you check if(!sentado.isChecked()&&!livre.isChecked()) returns true (by default, no checkbox is clicked), even if I have clicked on a checkbox.

  • Look at it this way: if (!seated.isChecked()) && (!free.isChecked()) {

1 answer

4


The problem is that the variables sentado and livre refer to the Checkbox of another View and not the Checkbox of another View Alertdialog.

alert.setView(inflater.inflate(R.layout.layout_alert, null));

In the previous code is "inflated" a View and passed to Alertdialog.

Then with

View view = inflater.inflate(R.layout.layout_alert, null);
sentado = (CheckBox)view.findViewById(R.id.sentado);
livre = (CheckBox)view.findViewById(R.id.livre);

is "inflating" another View to obtain references to Checkbox.

What must be done is to pass the View, used to obtain references, to Alertdialog.

LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.layout_alert, null);
sentado = (CheckBox)view.findViewById(R.id.sentado);
livre = (CheckBox)view.findViewById(R.id.livre);

AlertDialog.Builder alert = new AlertDialog.Builder(novo_layout.this);
alert.setCancelable(false);
alert.setView(view);
alert.setMessage(IdAsString);
  • Exactly! Much easier to understand, thank you

  • A question I’ve come up with now by completing my code, when I’m 5 checkboxes selected, and want to desiccate them I will only be able to access the values of checkboxes seated and free from the last checkbox selected. How can I resolve this situation? If necessary I can edit the code to make it clearer, @ramaral

  • Try to explain it better because I didn’t understand what you mean by: "I will only be able to access the values of the sitting and free checkboxes of the last checkbox selected".

  • I already edited the code and I think it is already better explained @ramaral

  • That’s another problem, you better open up another question.

Browser other questions tagged

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