-1

How to check that if the given name exists in "FacultyMember" record. If the given name is found in the record then I want to retrieve its parent (that is uid) other show a message name not found. How can I do it in the android studio? This is my database structure. Thanks

[This is my database Structure.

PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
Abbas Ali
  • 165
  • 3
  • 9

1 Answers1

2

What you can do in this case is, order your database concerned child with Name and compare them to the nameYouWant, and using dataSnapshot.exists() decide what you have to do with the user.

What I am saying, looks something like this in code:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("FacultyMember");
databaseReference.orderByChild("Name").equalTo(nameYouWant).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()){
          for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
               String uid = childSnapshot.getKey();
        }
        else
            // no such user exists

    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) { // ToDo: Do something for errors too

    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20