提取 Firebase 实时数据库路径中的第二个 ID,使用 Java 如何做?

huangapple go评论66阅读模式
英文:

How to extract the second ID from a Firebase Realtime Database path using Java?

问题

从Firebase实时数据库获取数据

如何从以下路径中获取第二个id:/Requests/krZF9AMNmwZO6cxuqZFaVJA8ZNg1/x0PTLSiX61MGNfkfVVnjsn9VsZy1?如果第一个id是发送请求的用户,第二个id应该获取它并输出其中的数据。第一个和第二个id只是示例,它们可能不同。

提取 Firebase 实时数据库路径中的第二个 ID,使用 Java 如何做?

英文:

Getting data from FireBase RealTime Database

How can I get the second id from the following path: /Requests/krZF9AMNmwZO6cxuqZFaVJA8ZNg1/x0PTLSiX61MGNfkfVVnjsn9VsZy1? if the first id is the user who sent the request and the second one should get it and output data from it. The first and second id were chosen as an example they may differ.

提取 Firebase 实时数据库路径中的第二个 ID,使用 Java 如何做?

答案1

得分: 0

如果您知道第一级ID(krZF9...),您可以读取该节点下的所有数据,并循环遍历子节点:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Requests/krZF9AMNmwZO6cxuqZFaVJA8ZNg1");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot secondSnapshot: dataSnapshot.getChildren()) {
            String key = secondSnapshot.getKey(); // x0PTLSiX61MGNfkfVVnjsn9VsZy1
            String status = secondSnapshot.child("status").getValue(String.class);
            // ...
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
});
英文:

If you know the first level ID (krZF9...), you can read all data under that node, and loop over the children with:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Requests/krZF9AMNmwZO6cxuqZFaVJA8ZNg1");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot secondSnapshot: dataSnapshot.getChildren()) {
            String key = secondSnapshot.getKey(); // x0PTLSiX61MGNfkfVVnjsn9VsZy1
            String status = secondSnapshot.child("status").getValue(String.clasS);
            ...
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
});


huangapple
  • 本文由 发表于 2023年5月21日 22:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定