如何从firebase数据库中的嵌套节点检索所有数据

时间:2017-05-09 23:47:57

标签: android firebase firebase-realtime-database

我想从我的数据库中获取所有值。但问题是我无法使数据库引用正确或数据快照的for循环以获取所有值。结果始终为null且没有错误。 这是我的代码:

      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

我想获取所有日期节点的值。 这是我的databaseReference

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

由于引用而使用此产品和空列表。

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

但是这只显示了05-10-2017的数据

请帮我从REPORTS.TIA

下的子节点获取所有数据

1 个答案:

答案 0 :(得分:2)

你可以试试这个......

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) {

        }
}

你可以尝试这个...... 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) {

        }
}