Kizzle
Kizzle

Reputation: 101

Retrieve data from Firebase as list

I want to be able to retrieve everything that is currently held in a Firebase child folder. I write to the database like so;

    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();
}

I want to be able to read all values that are currently held in the child folder and then eventually display them in a ListView.

Database JSON;

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

Upvotes: 0

Views: 61

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

To get the data only from a particular node, please use the following code:

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);

Your output will be:

none now!, testing,

Upvotes: 2

Related Questions