2017-07-14 60 views
0

我有這樣的數據庫enter image description hereAndroid - Firebase |閱讀所有的子數據

大,中,小可以有任何數量的孩子(abc,xyz等)。我想讀一個給定節點的所有孩子(比如小)。

final FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference myRef = database.getReference("small"); 

// Read from the database 
myRef.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     // This method is called once with the initial value and again 
     // whenever data at this location is updated. 
     Log.i("Info","Data changed"+Long.toString(dataSnapshot.getChildrenCount())); 

     for (DataSnapshot dis : dataSnapshot.getChildren()) { 
      for (DataSnapshot vers : dis.getChildren()) { 
       String value = vers.getValue(String.class); 
       Log.d("debug", "Value is: " + value); 
       toaster(value); 
      } 
     } 
    } 

但是我沒有輸出。我無法想象如何遍歷這些數據。

請幫

+0

你的代碼使用'small',數據庫有'Small'。由於Firebase區分大小寫,因此兩個字符串並不相同。如果您在代碼中使用相同的大小寫,它將起作用。投票結束爲錯字。 –

回答

0

爲了解決這個問題,你需要改變這一行:

DatabaseReference myRef = database.getReference("small"); 

DatabaseReference myRef = database.getReference().child("Small"); 

這將解決您的問題。

+0

我很抱歉,但沒有奏效。 「小」可以有孩子,反過來可以有很多孩子。我想獲得這些值 –

+0

當我再次閱讀你的代碼時,我得到了錯誤。請看我更新的答案。它應該是:'database.getReference()。child(「Small」);'它工作嗎? –

+0

我做了改變,但仍然沒有輸出。並且getChildren計數返回0! –