mardi 28 mai 2019

How to show alertdialog for evey child in firebase has true in it

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) {

                }
            });

        }
    });
}

once it is true a dialog will show once it is false dialog will not be shown and automatically will be directed to doProgramActivity

Aucun commentaire:

Enregistrer un commentaire