3

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

I have two problems to tackle:

  • I need to check if a school from my database exists before continuing.
  • I need to verify that a username entered by a user isn't already in use (all lowercase).

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.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Edan
  • 39
  • 1
  • 2

1 Answers1

3

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)?

Saurabh Padwekar
  • 3,888
  • 1
  • 31
  • 37