我很难阅读我的Firebase上的更改。我想做的是,每当触发启动按钮时进行一次更改,就会出现一个对话框,显示“此操作已被执行”。
我尝试使用addListenerForSingleValueEvent,但似乎对话框仅在所有子项都已更改时显示。
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
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) {
}
});
答案 0 :(得分:1)
您需要更改此内容
databaseReference.addListenerForSingleValueEvent(new ValueEventListener
对此:
databaseReference.addValueEventListener(new ValueEventListener
从文档中
public abstract void onDataChange (DataSnapshot snapshot)
将使用此位置的数据快照调用此方法。每次数据更改时也会调用它