Android beginner here. This may sound very silly, but I'm having issues with an if-else block that I created in my onDataChange() method in an event listener for a firebase database-reference.
Here's the code for the listener:
requestRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot request : dataSnapshot.getChildren()){
RequestDetails retrievedDetails = request.getValue(RequestDetails.class);
if(retrievedDetails.equals(requestDetails)){
alreadyRequested = true;
// finish();
// startActivity(getIntent());
break;
}
}
}
if(!alreadyRequested){
//alreadyDisplayed = true;
mDatabase.child("Requests").push().setValue(requestDetailsHashMap);
Toast.makeText(ParticipantSportOptions.this, "Request Successfully Sent!", Toast.LENGTH_SHORT).show();
// finish();
// startActivity(getIntent());
}
else if(alreadyRequested){
Toast.makeText(ParticipantSportOptions.this, "Request has already been received!\nPlease wait for approval!",
Toast.LENGTH_SHORT).show();
return;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
This listener is inside the overridden method onClick() for a button. What needs to happen is this - the first time this button is clicked, data (a request) is written into the database. For every subsequent click of the button, I check if the user is attempting to send multiple requests and if so I display the toast and return.
What I observe during run-time is that on the first click of the button, both toast messages are displayed.
Why does this happen?
Aucun commentaire:
Enregistrer un commentaire