Edan
Edan

Reputation: 39

Android Firebase database, check if values exist

I'm still learning the basics of android and firebase coding.

I have two problems to tackle:

My database looks like this:

app: {
    school: {
        school: "university one"
        school: "university two"
        school: "university three"
    },
    users: {
        "some-user-uid": {
            school: "university one"
            username: "myname"
        }
    }
}

Thank you.

Upvotes: 3

Views: 5825

Answers (1)

Saurabh Padwekar
Saurabh Padwekar

Reputation: 4074

To get your firebase Database reference :

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

To check if the username exists or not :

String username= "myname" ;
mDatabase.child("app").child("user").child("uid").orderByChild("username").equalTo(username).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
      if(dataSnapshot!=null && dataSnapshot.getChildren()!=null &&                                         
          dataSnapshot.getChildren().iterator().hasNext()){                     
               //Username exists
             }else {
               //Username does not exist
              }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
               //Error
        }
    });

For school name I wonder how can all the school values have the same key(school)?

Upvotes: 3

Related Questions