gpt4 book ai didi

java - Firebase 读取每个条目中的子目录

转载 作者:行者123 更新时间:2023-12-02 01:53:48 24 4
gpt4 key购买 nike

如何获取数据库中所有“Floor”元素的列表。如果我使用以下数据库,我的列表应该有 3 个元素。列表应包含 id 为 1234、4321、2341 的元素。

 mydatabase
-buildings
--LNBxRoNBhVZyXniqe9t
---checked
---anzI
---anzA
---floors
----f1
-----1234
------description
------id
----f2
-----4321
------description
------id

--LXdsafRfasdf12asdfJ
---checked
---anzI
---anzA
---floors
----f1
-----2341
------description
------id

This is my DAO:

     private FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("buildings");
@Override
public void initialize() {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
List<Floors> list = new LinkedList<>();

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
????
}

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

最佳答案

您需要遍历从 Firebase 获取的 DataSnapshot。像这样的事情

myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot buildingSnapshot: dataSnapshot.getChildren()) {
for (DataSnapshot floorSnapshot: buildingSnapshot.child("floors").getChildren()) {
for (DataSnapshot numberSnapshot: floorSnapshot.getChildren()) {
Log.i(TAG, numberSnapshot.getKey()); // "1234", "4321", "2341"
Log.i(TAG, ""+numberSnapshot.child("id").getValue());
}
}
}
}

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

关于java - Firebase 读取每个条目中的子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52594564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com