Get Firebase data from certain child

I'm starting to code Android Apps with the database connection. As IDE I use Android Studio and Firebase for the database. This is my data structure in Firebase: enter image description here

In my app, I need to fill a ListView with all the names. So I want the data from three children from the child "Rezepte" from the UUID child. I hope this is understandable.

I've done a lot of googling and I just can't figure out how this works. My current version of the java code is this:

final FirebaseUser user = firebaseAuth.getCurrentUser();
    // trying to get the reference of the right path
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(user.getUid()).child("Rezepte");
    ref.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    // calling a method which should read the data from the snapshot and put it into the ListView
                    fillList((Map<String, Object>) dataSnapshot.child(user.getUid()).child("Rezepte").getValue());
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Toast.makeText(RezepteActivity.this, "Daten konnten nicht gelesen werden", Toast.LENGTH_SHORT).show();
                }
            });
}

private void fillList(Map<String, Object> entries) {

    // loop to cycle through every child of "Rezepte" and get the names
    for (Map.Entry<String, Object> entry : entries.entrySet()) {
        Map singleEntry = (Map) entry.getValue();
        // add the name to an ArrayList of all names
        Rezepte.add((String) singleEntry.get("name"));
    }
    // put the data into the ListView
    lvRezepte.addFooterView(lvRezepte, Rezepte, true);
}

The app always crashes and I don't really understand a lot in the debugger. But the error occurs already in the onDataChange void.

I would be really happy if someone could help me.

3 answers

  • answered 2017-11-12 20:24 urgentx

    You're trying to traverse down the hierarchy in your result DataSnapshot when you've already done so in your query itself. So in your onDataChange() you need to just put:

    fillList(datasnapshot.getValue()) //Map of results at /{uid}/Rezepte
    

  • answered 2017-11-12 20:29 Catalin Ghita

    Assuming that fillList method works properly, you should first fix the following line

    fillList((Map<String, Object>) dataSnapshot.child(user.getUid()).child("Rezepte").getValue())
    

    to

    fillList((Map<String, Object>) dataSnapshot.getValue())

    You already have a reference to Rezepte child of the current user when you declared it like this: DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(user.getUid()).child("Rezepte")

    Your snapshot has 1, 2, and 3 as children, so you shouldn't reinforce the path you declared in the reference.

  • answered 2017-11-13 12:17 Alex Mamo

    To get all those names and add them to an ArrayList, please use the following code:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference rezepeteRef = rootRef.child(user.getUid()).child("Rezepte");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<String> list = new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("name").getValue(String.class);
                list.add(name);
                Log.d("TAG", name);
            }
            Log.d("TAG", list);
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    rezepeteRef.addListenerForSingleValueEvent(eventListener);