2017-02-16 91 views
2

這是我在firebase中的數據庫結構。如何訪問嵌入在Firebase數據庫中的子節點內的子節點?

enter image description here

我要訪問的消息下的child.I消息內容可以得到數孩子消息的孩子,但不能檢索每個孩子的價值。

ref2指向我的firebase實例。
我必須從particulat聊天室獲取所有消息。

 ref2.addListenerForSingleValueEvent(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) { 
         listText.add(childDataSnapshot.child("messages").child("message").getValue() + ""); 
         listText.add(childDataSnapshot.child("messages").child("name").getValue() + ""); 
         // userArrayList.add(new User("" + childDataSnapshot.child("username").getValue(), "" + childDataSnapshot.child("profileURL").getValue(), "")); 
         Log.v("messag_ids", "" + childDataSnapshot.child("messages").child("Kd4TQPrAWsNY1vcid1P").child("message")); //displays the key for the node 

        } 
        lvChat.setAdapter(new ChatAdapter(ChatActivity.this, listUsername, listText)); 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 

       } 
      }); 
+0

發佈您的代碼請 –

+0

代碼更新。 –

+0

您正在使用錯誤的參考。檢查答案。 –

回答

-1

如果你想得到消息的孩子,我們需要像這樣遍歷。

'Kd4TN8fuda6CArtAUo0' - > '消息' - > 'Kd4TQPrAWsNY1vcid1P' - > '消息'

嘗試這種情況:

<yourref>.child('Kd4TN8fuda6CArtAUo0').child('messages').child('Kd4TQPrAWsNY1vcid1P').child('message') 

enter image description here

參考此鏈接由FireBase團隊:Common SQL Queries converted for the Firebase Database - The Firebase Database For SQL Developers #4

+0

的值爲空。 –

+0

你是否檢查過我的答案中的鏈接,它是由FireBase團隊檢查的。 –

+0

是的,我在這裏使用相同的查詢。 –

0

您[R問題就在這裏:

childDataSnapshot.child("messages").child("message") 

你正在尋找一個叫做messagemessages節點,它不存在。

您應該首先參考messages節點,然後遍歷所有孩子。

首先appoach是使用方法DataSnapshot遍歷孩子 和訪問值使用:

ref2.addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) { 

        String name = (String) childDataSnapshot.child("name").getValue(); 
        //Do the same for other properties. 

       } 
       //...... 
    } 
    //.. 
} 

否則第二種方法是使用內置JSON對POJO串行器/解串器。
您可以使用Message.class,然後使用

ref2.addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) { 
       Message message = messageSnapshot.getValue(Message.class); 
      } 
     } 
    //.. 
} 

Message類應該是這樣的:

public class Message { 

    String name; 
    String message; 
    String chatroom; 

    public Message(){ 
    //default constructor 
    } 

    //Getters and setters 
    //.... 

} 
+0

我試過了這兩個,但沒有幫助。我的ref refrence是這個 ref2 = mDatabase1.child(「chatrooms」)。orderByChild(「roomName」)。equalTo(「s」); –

+0

@GyanendraMani你的參考應該是聊天室內的消息nome。 –

+0

這樣的事情: DatabaseReference df = mDatabase.child(「Kd4TN8fuda6CArtAUo0」)。child(「messages」); df.addListenerForSingleValueEvent(新ValueEventListener(){} 其中 mdatabse指向mDatabase = FirebaseDatabase.getInstance()getReference( 「聊天室」);。 –

0

我有更多的答案很簡單:

您嘗試訪問消息像其他節點像chatpic,所有者等... // Ur Databas Ë引用父節點,當你企圖爲孩子使用下面的代碼來訪問消息:

for(DataSnapshot dataSnapshot1:mainSnapShot.child("message").getChildren()){       

          Map<?, ?> valuex = (Map<?, ?>) dataSnapshot1.getValue(); 


          if (!TextUtils.isEmpty(valuex.get("message").toString())) { 
           Log.i("message",valuex.get("message").toString()); 
          } 



          if (!TextUtils.isEmpty(valuex.get("name").toString())) { 
           Log.i("name",valuex.get("name").toString()); 
          } 

         } 
相關問題