2017-09-07 150 views
0

我希望能夠檢索當前在Firebase子文件夾中保存的所有內容。我像這樣寫入數據庫;從列表中檢索數據

private void commitPost() { 
    final String commentInput = commentText.getText().toString().trim(); 
    commentProgress.setMessage("Posting"); 
    commentProgress.show(); 

    String uniqueId = UUID.randomUUID().toString(); 
    commentDB.child(postID).child("Comments").child(uniqueId).setValue(commentInput); 
    commentProgress.dismiss(); 
    finish(); 
} 

我希望能夠讀取當前的子文件夾保存,然後在ListView最終顯示它們的值。

數據庫JSON;

"Engagement" : { 
"-Kne46iBe6ooNFKTv_8w" : { 
    "Comments" : { 
    "c323835c-290c-44f3-8070-f9febf698ec9" : "none now!", 
    "stg15QKZFhNmTCYrgL5PtQ4wxJf2" : "testing" 
    }, 
    "Likers" : { 
    "stg15QKZFhNmTCYrgL5PtQ4wxJf2" : "[email protected]" 
    } 
+0

請發表您的數據庫結構。 –

+0

@AlexMamo完成。 – Kizzle

+0

哪個子文件夾? –

回答

2

要僅從特定節點獲取數據,請使用如下代碼:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); 
DatabaseReference engagementRef = rootRef.child("Engagement").child("-Kne46iBe6ooNFKTv_8w").child("Comments"); 
ValueEventListener eventListener = new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     for(DataSnapshot ds : dataSnapshot.getChildren()) { 
      String value = ds.getValue(String.class); 
      Log.d("TAG", value + ", "); 
     } 
    } 

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

你的輸出將是:

none now!, testing,