【问题标题】:Android Firebase: Retrieve IDAndroid Firebase:检索 ID
【发布时间】:2018-03-21 06:58:31
【问题描述】:

如何在 firebase 数据库中获取项目“001”的 ID。我手动创建了这个数据库

 "movies" : {
    "action" : {
      "001" : {
                "name" :    "Firebase",
                "director": "Google"  
              }
               }
            }

【问题讨论】:

    标签: android json database firebase firebase-realtime-database


    【解决方案1】:

    firebase 以 json 格式存储数据,表示为密钥对值。

    因此,如果您想检索表示数据结构中的键的项目“001”。

    要获得“001”,您可以使用 Firebase addListenerForSingleValueEvent 。

    DatabaseReference mDatabaseReference =FirebaseDatabase.getInstance().getReference().child("movies").child("action");
    mDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.getValue() != null) {
                        HashMap mapRecord = (HashMap) dataSnapshot.getValue();
                        Iterator listKey = mapRecord.keySet().iterator();
                        while (listKey.hasNext()) {
                            String id = listKey.next().toString();
                       //you get you key here    
                        }
                    }
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
    
                }
            });
    

    【讨论】:

    • 您在代码中的 JSON 模型中缺少 001。我还建议不要转换为地图,而是使用dataSnapshot.getChild("name").getValue(String.class) 来获取属性。
    猜你喜欢
    • 2018-07-27
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2017-06-10
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    相关资源
    最近更新 更多