Programming
Android alertDialog

Question Description

I’m studying and need help with a Programming question to help me learn.

Hi, what's wrong with this code, the second if doesn't work

@SuppressWarnings("deprecation")

public void showAnulacao(View v) {

String result = "Os dias anulados foram:";

Double totalAmount = 0.0;

for (Refeicao p : boxAdapter.getBox()) {

if (p.selected) {

result += "\n" + p.dia;

totalAmount += p.preco;

AlertDialog alertDialog = new AlertDialog.Builder(CantinaDiurnoActivity.this).create();

alertDialog.setTitle("Informação");

alertDialog.setMessage(result + "\n" + "Creditamos-lhe: " + totalAmount + "€");

alertDialog.setIcon(R.drawable.tick);

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

finish();

}

});

alertDialog.show();

}

if(!p.selected){

AlertDialog alertDialog = new AlertDialog.Builder(CantinaDiurnoActivity.this).create();

alertDialog.setTitle("Erro!");

alertDialog.setMessage("Não efetuou nenhuma seleção!");

alertDialog.setIcon(R.drawable.delete);

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

finish();

}

});

alertDialog.show();

}

}

}


Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.

Final Answer

Why not replace the second if with a simple 'else'?

When you say it is not working, what is happening?

BradJones (41)
University of Virginia

Anonymous
The tutor managed to follow the requirements for my assignment and helped me understand the concepts on it.

Anonymous
The tutor was knowledgeable, will be using the service again.

Anonymous
Awesome quality of the tutor. They were helpful and accommodating given my needs.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors