I would like to check if the user in the database exists. with this line of codes, it says that it exists and also that it does not exist. I want to make the code read-only if the name exists in one of the registers
Firebase database
private void Criar_Conta() {
databaseReference_users.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (snapshot.child("usuario").getValue().equals(Usuario.getText().toString()) && snapshot.getKey().equals(Usuario.getText().toString()) && snapshot.getKey().equals(snapshot.child("usuario").getValue())) {
Toast.makeText(Sign_Up.this, "Usuário Existente", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Sign_Up.this, "Gravar ...", Toast.LENGTH_SHORT).show();
//Gravar_Dados();
}
}
} else {
Gravar_Dados();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(Sign_Up.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
Swipe.setRefreshing(true);
}
});
2
Answers
This is my code sample I hope this helps you
To be able to check if a specific user exists in the database, you should only perform a get(), and not attach a ValueEventListener. Assuming that the users are direct children of your Firebase Realtime Database root, in code, will be as simple as: