英文:
How to extract the second ID from a Firebase Realtime Database path using Java?
问题
从Firebase实时数据库获取数据
如何从以下路径中获取第二个id:/Requests/krZF9AMNmwZO6cxuqZFaVJA8ZNg1/x0PTLSiX61MGNfkfVVnjsn9VsZy1
?如果第一个id是发送请求的用户,第二个id应该获取它并输出其中的数据。第一个和第二个id只是示例,它们可能不同。
英文:
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.
答案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();
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论