我如何从firebase实时数据库中获取所有数据

时间:2022-10-31 11:43:44

I have a database with this schema:

我有一个这个架构的数据库:

我如何从firebase实时数据库中获取所有数据

I tested this code but I can't get any msg from the database. I would appreciate your help.

我测试了这段代码,但我无法从数据库中获取任何消息。我很感激你的帮助。

 private void Add_Chat(final DataSnapshot dataSnapshot) {
        root.child("ChatSpace").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                String nom=dataSnapshot.child("ChatSpace").child("msg").getValue(String.class);
                System.out.println(nom);  //prints "Do you have data? You'll love Firebase."
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

1 个解决方案

#1


0  

Assuming that ChatSpace is a direct chlid of your Firebase database root, to get all messages please use the following code:

假设ChatSpace是Firebase数据库根目录的直接chlid,要获取所有消息,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference chatSpaceRef = rootRef.child("ChatSpace");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String msg = ds.child("msg").getValue(String.class);
            Log.d("TAG", msg);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
chatSpaceRef.addListenerForSingleValueEvent(eventListener);

Your output will be:

你的输出将是:

bbbName
bbbName
Name

#1


0  

Assuming that ChatSpace is a direct chlid of your Firebase database root, to get all messages please use the following code:

假设ChatSpace是Firebase数据库根目录的直接chlid,要获取所有消息,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference chatSpaceRef = rootRef.child("ChatSpace");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String msg = ds.child("msg").getValue(String.class);
            Log.d("TAG", msg);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
chatSpaceRef.addListenerForSingleValueEvent(eventListener);

Your output will be:

你的输出将是:

bbbName
bbbName
Name