[英]Getting all keys from a firebase tree
This is my firebase tree. 这是我的火力树。
"LAMBO" : {
"LAMBO" : {
"colour" : "blue",
"date" : "08 04 2018",
"doors" : "2",
"enginesize" : "3000",
"fuel" : "petrol",
"lotnumber" : "99",
"manufacturer" : "Lamborghini",
"model" : "cuntash",
"reg" : "LAMBO"
}
}
"FIESTA" : {
"FIESTA" : {
"colour" : "red",
"date" : "08 04 2018",
"doors" : "5",
"enginesize" : "1300",
"fuel" : "petrol",
"lotnumber" : "1",
"manufacturer" : "ford",
"model" : "fiesta",
"reg" : "FIESTA"
}
}
etc etc.... 等等等等...
as the user adds more cars to the tree obviously the list will get bigger. 随着用户向树中添加更多汽车,列表显然会越来越大。 I want to be able to get a list of all the items in my tree.
我希望能够获得树中所有项目的列表。 But I won't know the key name as I won't be in control of the key the user has.
但是我不知道密钥名称,因为我无法控制用户拥有的密钥。 The Key is = to The cars Registration so is unique every time.
关键是汽车注册的=,因此每次都是唯一的。
the way I normally access the child would be 我平常接触孩子的方式是
String lambo = datasnapshot.child("LAMBO").getValue(String.class)
from within a ChildEventListener
. 从
ChildEventListener
。 But I won't know the ("KEYNAME") 但我不知道(“ KEYNAME”)
I've found this snippet of code 我已经找到了这段代码
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
int i = 0;
for(DataSnapshot d : dataSnapshot.getChildren()) {
}
}
}//onDataChange
@Override
public void onCancelled(DatabaseError error) {
}//onCancelled
});
I just want the Main keys in my list view ie LAMBO and FIESTA not the children from it either. 我只希望列表视图中的Main键(即LAMBO和FIESTA)而不是它们的子项。 any help would be appreciated and sorry for being confusing
任何帮助将不胜感激,并感到困惑
You can use getKey() from DataSnapShot: 您可以使用DataSnapShot中的getKey():
List<String> keyList = new ArrayList<>();
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
keyList.add(ds.getKey());
}
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.