Search code examples
androidfirebasefirebase-realtime-databasedata-retrieval

Unable to retrieve multiple data from firebase into card view/ list view


I am working on a firebase application & i got stuck while retrieving the database into cardview/listview.

Here is a demo look to the firebase database.

   db
   |- 001
       |- details
            |- key1: "value1"

            |- key2: "value2"

            |- key3:
                |- key3_1: "value3_1"

            |- key4:
                |- key4_1: "value4_1"  

This is a demo database but exactly the same as per my requirements. Here i want to retrieve every key-value pair. I tried it by first passing the database reference then in child i mention "/db" based on above example. Then i tried to fetch the data but it didn't work for above example. Anyone interested out there please share your ideas on this!

Any answer or suggestion would be greatly appreciated!


Solution

  • Please use this code:

    DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("001").child("details");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String key1 = dataSnapshot.child("key1").getValue(String.class);
            String key2 = dataSnapshot.child("key2").getValue(String.class);
            //and so on
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    yourRef.addListenerForSingleValueEvent(eventListener);
    

    You output wil be:

    value1
    value2
    value3
    //and so on
    

    Hope it helps.