如何从Firebase实时数据库中正确检索价值?

时间:2017-07-03 15:15:31

标签: android firebase firebase-realtime-database

我有以下数据结构:

see here

name可以拥有无​​限数量的孩子。

以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();    
DatabaseReference dirFire = rootRef.child("messages").child("name");

 dirFire.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
    String name = dataSnapshot.getValue(String.class);
    //do what you want with the name
 }

 @Override
 public void onCancelled(DatabaseError databaseError) {

 }
 });

使用上面的代码,我无法检索name的值。如果客户端不知道name,我该如何检索key

2 个答案:

答案 0 :(得分:3)

请使用此代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();    
DatabaseReference messagesRef = rootRef.child("messages");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            String key = ds.getKey();

            DatabaseReference keyRef = rootRef.child("messages").child(key).child("name");
            ValueEventListener valueEventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot ds1 : dataSnapshot.getChildren()) {
                        String name = ds1.getValue(String.class);
                        Log.d("TAG", name);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {}
            };
            keyRef.addListenerForSingleValueEvent(valueEventListener);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
messagesRef.addListenerForSingleValueEvent(eventListener);

编辑:这是使用看起来像早期解决方案的另一种方法。

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();    
DatabaseReference messagesRef = rootRef.child("messages");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            for (DataSnapshot nameSnapshot : ds.child("name").getChildren()) {
                String name = nameSnapshot.getValue(String.class);
                Log.d("TAG", name);
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
messagesRef.addListenerForSingleValueEvent(eventListener);

输出将是:

John Miche
Joe Mitch
Sabr. A
Luis Jean

答案 1 :(得分:3)

将您的数据导入hashMap ..

  FirebaseDatabase.getInstance().getReference()
            .child("messages")
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot rootSnapshot) {
                    if (rootSnapshot.exists()) {
                        final Map<String, Map<String, String>> messageNameMap = new HashMap<>();
                        for (DataSnapshot messageSnapshot : rootSnapshot.getChildren()) {
                            final String messageKey = messageSnapshot.getKey();
                            HashMap<String, String> nameMap = new HashMap<String, String>();
                            if (messageSnapshot.child("name").exists()) {
                                for (DataSnapshot nameSnapshot : messageSnapshot.child("name").getChildren()) {
                                    final String nameKey = nameSnapshot.getKey();
                                    final String nameValue = nameSnapshot.getValue(String.class);
                                    nameMap.put(nameKey, nameValue);
                                }
                            }
                            if (!nameMap.isEmpty()) {
                                messageNameMap.put(messageKey, nameMap);
                            }
                        }

                        if (!messageNameMap.isEmpty()) {
                            // Here messageNameMap contains your data as such

                            // key: -Ko8bLawP4... val: Map Containing: 
                            //                          key: -Ko8wk... val: Jonathan Smith
                            //                          key: -Ko8wk... val: Kapler Moe
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });