如何在Android中从Firebase获取用户ID?

时间:2017-12-14 09:49:31

标签: android firebase firebase-realtime-database

I want to retrieve the circled part from my firebase database

我做了这个:

 adapter.getRef(currentPosition).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
           adapterKey =  dataSnapshot.getChildren().toString();
           Log.i("adapterKey",adapterKey);
        }

        @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) {

        }
    });

我想检索由firebase生成的ID,以便访问EndUsers节点的内部数据。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

试试这个

for(DataSnapshot snapshot:dataSnapshot.getChildren()){
//snapshot.getKey()
//snapshot.getValue()
}

答案 1 :(得分:1)

DatabaseReference mDatabaseReference; //you must initialize it 

mDatabaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            Log.i(TAG, "the key is : " + child.getKey());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});