0

This is my JSON data structure in Firebase database:

familylist
     |- 0
        |--children
        |   |-- 0:"AAA"
        |   |-- 1:"BBB"
        |-- code: "2222"
        |-- family: "SWE"
        |-- fatherName: "ABC"
        |-- motherName: "XYZ"
     |- 1
        |--children
        |   |-- 0:"XXX"
        |   |-- 1:"YYY"
        |-- code: "3333"
        |-- family: "ABC"
        |-- fatherName: "ERT"
        |-- motherName: "XTS"

I'm able to pull up all the fields except children from the JSON Tree. Can anyone suggest me how to retrieve the data from children ?

This is my code for retrieving all the fields from firebase database except children. What can be done to this code in order to retrieve the children values as well ? I'm not able to figure out.

familyListReference.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot ds: dataSnapshot.getChildren()) {
      String key = (String) ds.getKey();
      Log.d(TAG, "Key" + key);
      DatabaseReference keyReference = FirebaseDatabase.getInstance().getReference().child("familylist").child(key);
      Log.d(TAG, "Key reference: " + keyReference);
      keyReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
          String code = dataSnapshot.child("code").getValue(String.class);
          String family = dataSnapshot.child("family").getValue(String.class);
          String father = dataSnapshot.child("fatherName").getValue(String.class);
          String mother = dataSnapshot.child("motherName").getValue(String.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
          Log.d(TAG, "Read failed");
        }
      }); // [End of keyReference]

    } // END of for Loop
  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    Log.d(TAG, "Read failed");
  }
}); // [End of familyListDatabaseReference]

3 Answers 3

6

Since children is a list, you're probably looking for:

public void onDataChange(DataSnapshot dataSnapshot) {
  for (DataSnapshot childSnapshot: dataSnapshot.child("children").getChildren()) {
    System.out.println(childSnapshot.getValue(String.class));
  }
}

Note that the Firebase documentation recommends against using arrays for such lists, such as in this blog post. Instead it recommends using push() to populate lists.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect and flawless! Actually, I've imported an existing JSON file to firebase database and from there pulling up the data from Activity.
2

Add this lines of code in your onDataChange() method, after the declartation of those 4 strings:

      String code = dataSnapshot.child("code").getValue(String.class);
      String family = dataSnapshot.child("family").getValue(String.class);
      String father = dataSnapshot.child("fatherName").getValue(String.class);
      String mother = dataSnapshot.child("motherName").getValue(String.class);
      for (DataSnapshot ds : dataSnapshot.child("children").getChildren()) {
          Log.d("TAG", ds.getValue(String.class));
      }

Hope it helps.

4 Comments

In other nodes, I have 3 children. I can't hard code like this manually right?
I didn't know, and yes, you're right but i saw only 2 childrens in each node in your above example. So in order to solve this, please add those lines suggested by Frank van Puffelen.
Great! Worked for me!
Happy to hear that. Cheers!
1

If you actually don't know about how many children you have, I think my solution is better then the one previously suggested by another user.

List<String> children = dataSnapshot.child("children").getValue(new GenericTypeIndicator<List<String>>(){});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.