简体   繁体   中英

Android Firebase get other user's uid from realtime database

Is there any way to get another user's uid from realtime database? I want to get 2 name objects from the database with different uids at the same time.

Json file:

"Users" : {
  "PnZdR1Ily5R5R58FYFVEuWN0Q855" : {
     "-Kwwceeevvvvvttvv" : {
      "name" : "abc"
      }, 
     "-Kccrr325gdfgdfFg" : {
      "name" : "abc"
      }
    }, 
  "MeTsW4SsW6e8hGrsFfVUuNNaQ3cj" : {
     "-Kfvr345345354555" : {
      "name" : "zxc"
      }, 
     "-Kbfhf56464646646" : {
      "name" : "zxc"
      }
  }
}

This is my code:

ValueEventListener postListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                User details = dataSnapshot.getValue(User.class);
                mName = details.name;
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.w(TAG, "getUser:onCancelled", databaseError.toException());
            }
        };
mDatabase.child("Users").child($uid).addValueEventListener(postListener);

This is the part I'm stuck on, how do I get both uids at the same time?

If you want to retrieve random key then you should use addChildEventValueListerner because it gives u child of the Users node one by one.

 FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
 DatabaseReference mref = firebaseDatabase.getReference();
 mref.child("Users").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
              if (dataSnapshot.hasChildren()) {
                  User details = dataSnapshot.getValue(User.class);
              }

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM