'In Android, how to test the if of a code that is from the DatabaseReference class to see if the code returns true or false?
The following code shows what I really mean:
There is an object called on a reference, which belongs to the DatabaseRefence class, which calls the addValueEventListener method to test if a certain value exists in a table in Firebase. I'd like to test this block of code to see if it's true or false. If it is true (that is, if it does not exist, through the code: "if (!dataSnapshot.exists()) {"), then it prints a message: "Prints a message".
But if it's false (ie if it exists in Firebase Database), then do another action (which would be else of the if). But how to insert this code block inside the if, if it's not boolean type, but it is a DatabaseReference type?
The code is:
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
reference = FirebaseDatabase.getInstance().getReference().child("Users").child(cpfEmHash);
if (!dataSnapshot.exists()) {
Toast.makeText(getApplicationContext(), "Prints a message", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
if(reference) {
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

