如何从firebase数据库引用中获取子节点列表?

时间:2017-11-05 05:43:19

标签: android firebase arraylist firebase-realtime-database

DB Structure

下面我附上了DB Structure的图片,我试图从firebase数据库引用(timingInformation)获取子节点列表。我怎样才能获得清单?

1 个答案:

答案 0 :(得分:3)

要从Firebase数据库中读取/同步数据,您需要附加一个侦听器。一个简单的案例:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("timingInformation");
ref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
        Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
};

有关详情,请参阅Firebase documentation on loading data from a listreference documentation on what you can do with a snapshot