Victor
Victor

Reputation: 1205

Retrieve JSON array from Firebase in Android

I have a JSON array where I store a few items I want to display on my app's main menu. It looks like this:

enter image description here

I'd like to retrieve this list but for some reason I ignore the code below doesn't work:

DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mMainMenuRef = mRootRef.child("main_menu");

    @Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);

    mMainMenuRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String stringValue = ds.getValue(String.class);
                Log.i("Firebase", stringValue);
            }
        }

        ...

    });

Upvotes: 0

Views: 2148

Answers (2)

Ankit Patidar
Ankit Patidar

Reputation: 2781

Here is your modified code of yours, as per your requirement:

DatabaseReference mRootRef = 
FirebaseDatabase.getInstance().getReference();
DatabaseReference mMainMenuRef = mRootRef.child("main_menu");

@Override
protected void onStart() {
   super.onStart();
   mAuth.addAuthStateListener(mAuthListener);

   mMainMenuRef.addValueEventListener(new ValueEventListener() {
       @Override
       public void onDataChange(DataSnapshot dataSnapshot) {

          for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren())
            {

                for (DataSnapshot property :dataSnapshot1.getChildren()) {
                    String value = property.getValue(String.class);
                    Log.i("Firebase", value);
                }
            }
       }

    ...

});

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You reference points to /main_menu in the database:

DatabaseReference mMainMenuRef = mRootRef.child("main_menu");

You then listen to that reference and loop over the data:

mMainMenuRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            String stringValue = ds.getValue(String.class);
            Log.i("Firebase", stringValue);
        }
    }

This means that you loop over the child nodes directly under /main_menu, so -L-FDnRW.... You then try to get the string value of that node. But the -L-FDnRW... contains an entire JSON object, so there is no singular string value.

To get the value of a specific property under -L-FDnRW... use the DataSnapshot.child() method:

public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        String stringValue = ds.child("0").getValue(String.class);
        Log.i("Firebase", stringValue);
    }
}

To show the value of all children, loop over the child snapshots:

public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
      for (DataSnapshot prop : ds.getChildren()) {
        String stringValue = ptop.getValue(String.class);
        Log.i("Firebase", stringValue);
      }
    }
}

Upvotes: 1

Related Questions