0

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

        ...

    });

2 Answers 2

1

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);
      }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

    ...

});

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.