简体   繁体   English

从Firebase树获取所有密钥

[英]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.

相关问题 无法从Firebase获取RecyclerView的所有结果 - Not getting all results for RecyclerView from firebase Android Firebase-没有将所有键作为列表获取,我该怎么办? - Android Firebase- Not getting all keys as a list, what should I do? Tree中的所有节点均未扩展 - All the nodes in Tree not getting expanded 查询以从 Firebase 数据库中读取除特定键之外的所有键的所有数据? - query for to read all the data of all keys except a specific one from Firebase Database? Firebase UI:从子节点接收所有密钥并使用它从另一个节点接收信息并将其填充到 Recyclerview 中的解决方案? - Firebase UI: Solution to receive all keys from a child and use it to receive information from another node and fill it in a Recyclerview? Firebase-在子等于的情况下获取所有键 - Firebase - Get all keys where child equals 如何在不从参考节点获取所有数据的情况下获取 Firebase 数据库中的随机键? - How to get random keys in Firebase Database without fetching all data from a reference node? 从Firebase获取价值 - Getting values from Firebase 从 Java 瘦客户端从 Apache Ignite 缓存中获取所有密钥 - Getting All keys from Apache Ignite cache from Java Thin client 让所有孩子都处于二叉树的同一级别 - Getting all children on same level of binary tree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM