DarknessNight
DarknessNight

Reputation: 37

How to retrieve all data from nested nodes in firebase database

I would like to get all the values from my database. But the problem is that I can't make the database reference correct or the for loop of the datasnapshot in order to get all the values. The outcome was always null and without errors. Here is my code:

      databaseReports.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            reportList.clear();

            for(DataSnapshot userSnapshot : dataSnapshot.getChildren()){
                Reports reports = userSnapshot.getValue(Reports.class);
                reportList.add(reports);
            }
            ReportList adapter = new ReportList(ViewReports.this, reportList);
            listViewReports.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

Here is my database

I want to get the value of the all the date nodes. here is my databaseReference

    FirebaseDatabase.getInstance().getReference("REPORTS")

Using this produce and empty list because of the referencing.

    databaseReports = FirebaseDatabase.getInstance().getReference("REPORTS/05-10-2017");

But this one only shows the data under 05-10-2017

please help me get all the data from sub nodes under REPORTS.TIA

Upvotes: 2

Views: 1411

Answers (1)

1van
1van

Reputation: 101

You may try this...

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

            reportList.clear();

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

                Log.d("ymdSnapshot", ymdSnapshot.getKey().toString());

                for(DataSnapshot repSnapshot : ymdSnapshot.getChildren()){
                    Reports reports = repSnapshot.getValue(Reports.class);
                    reportList.add(reports);
                }
            }
            ReportList adapter = new ReportList(ViewReports.this, reportList);
            listViewReports.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
}

You may try this... partII

databaseReports.child("05-09-2017").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            reportList.clear();

            for(DataSnapshot repSnapshot : dataSnapshot.getChildren()){
                    Reports reports = repSnapshot.getValue(Reports.class);
                    reportList.add(reports);
            }

            ReportList adapter = new ReportList(ViewReports.this, reportList);
            listViewReports.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
}

Upvotes: 2

Related Questions