How to recover value of an integer and set in a alertDialog

Asked

Viewed 72 times

0

When opening alertDialog the result is set to zero, follow the code

calcular.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String adulto = edit_adulto.getText().toString();
            String crianca = edit_crianca.getText().toString();

            dialog = new AlertDialog.Builder(MainActivity.this);
            dialog.setTitle("Resultado");                
            dialog.setMessage("O valor é: " + resultadoA);
            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            if (dia.isChecked()){

                if(!adulto.isEmpty()){
                    valorDigitadoA = Integer.parseInt(adulto);
                    resultadoA = valorDigitadoA * 15;
                    dialog.show();
                }
                if(!crianca.isEmpty()){
                    valorDigitadoC = Integer.parseInt(crianca);
                     resultadoC = (valorDigitadoC * 10);
                }
            }

2 answers

2


You must use the method setMessage within the condition just after assigning the desired value to its variable. See how it should look:

if(!adulto.isEmpty()){
    valorDigitadoA = Integer.parseInt(adulto);
    resultadoA = valorDigitadoA * 15;

    // aqui será mostrado a mensagem no dialogo 
    // com o valor atribuído a variável resultadoA
    dialog.setMessage("O valor é: " + resultadoA);
    dialog.show();
}
  • 1

    gave right friend, thanks for the help!

1

Nothing appears because you are setting the Dialog text with the variable ResultadoA before it has any value.

Browser other questions tagged

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