After executing an exercise, I wanted to show an alert dialog that says, "You have already executed this. Are you sure you want to do it again?"
So in my firebase, once the exercise has already been executed isProgramExerciseFinished will be set to true. But in my code, it seems that the dialog only shows once all of it is set to true.
public void onClick(View view) { currentWeek = programTrackers.get(0).getWeek(); DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("ProgramTracker") .child(GlobalUser.getmUser().getiD()) .child(programTrackers.get(0).getProgramId()); databaseReference.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { final int childCount = (int) dataSnapshot.getChildrenCount(); int count = 0; for (DataSnapshot snapshot : dataSnapshot.getChildren()) { ProgramTracker programTracker = snapshot.getValue(ProgramTracker.class); if (programTracker.getProgramExerciseWeek() == currentWeek) { programTrackChecker.add(programTracker);
}
count++;
}
if (count == childCount) {
boolean isFinished = false;
//last iteration
for (int i = 0; i < programTrackChecker.size(); i++) {
if (programTrackChecker.get(i).isProgramExerciseFinished()) {
isFinished = true;
} else
isFinished = false;
}
if (isFinished) {
AlertDialog.Builder builder = new AlertDialog.Builder(DayExerciseActivity.this);
builder.setMessage("You have already completed all exercises. Are you sure you want to do it again?");
builder.setPositiveButton("Yes", (dialogInterface, i) -> {
startActivity(new Intent(DayExerciseActivity.this, DoProgramActivity.class)
.putExtra("programTrackers", programTrackers)
.putExtra("exerciseNum", String.valueOf(0)));
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.show();
} else
startActivity(new Intent(DayExerciseActivity.this, DoProgramActivity.class)
.putExtra("programTrackers", programTrackers)
.putExtra("exerciseNum", String.valueOf(0)));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
Aucun commentaire:
Enregistrer un commentaire