如何设计从Firebase提取的数据

时间:2018-11-18 03:05:37

标签: android firebase-realtime-database

尽管这个问题来自基础。我想在回收者视图中打印用户的所有子级,如何使Firebase看起来像这样

Root
        |
        |_users
                    |
                    |_john
                    |_paul
                    |_//other names....

1 个答案:

答案 0 :(得分:1)

尝试如下

DatabaseReference mDatabase;

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


mDatabase.child("users").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for (DataSnapshot postSnapshot: snapshot.getChildren()) {
            String name = postSnapshot.getValue();
            //do what you want
    }

    @Override
    public void onCancelled(DatabaseError error) {
        // Failed to read value
        Log.w(TAG, "Failed to read value.", error.toException());
    }
});