Skip to content
Advertisement

how can i get all the data in document firestore?

current i am working to my school project using android studio, it is an attendance system where I store my data to Firestore and the user are able to download/export the data to become Excel file. What I am trying to do is how can I get the all data in a single document of a Collection in firestore

here’s the code but it is only getting the first data in a document and it is showing in all the rows

 export.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        firebaseFirestore.collection("QR").document("QRScanned").collection(LoginProfessorTabFragment.superName)
                .document(TeacherDash.subjectName1).collection("Record of Attendance")
                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()){
                    HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
                    HSSFSheet hssfSheet = hssfWorkbook.createSheet(TeacherDash.subjectName1);

                    for (int i = 0; i < 4; i++) {//for creating equal amount of row from the database
                        HSSFRow row = hssfSheet.createRow(i);

                        for (int j = 0; j <= cellCount; j++) {//creating each cell depends on the cell counter
                            for (DocumentSnapshot documentSnapshot : task.getResult()){

                                String a = documentSnapshot.getString("Name");
                                String b = documentSnapshot.getString("Date");
                                String c = documentSnapshot.getString("Time");
                                String d = documentSnapshot.getString("StudentNumber");
                                String e = documentSnapshot.getString("Course");
                                String f = documentSnapshot.getString("Subject");
                                String g = documentSnapshot.getString("Room");
                                String h = documentSnapshot.getString("Schedule");

                                arrayExport.add(a);
                                arrayExport.add(b);
                                arrayExport.add(c);
                                arrayExport.add(d);
                                arrayExport.add(e);
                                arrayExport.add(f);
                                arrayExport.add(g);
                                arrayExport.add(h);

                                arrayRemoveAll.add(a);
                                arrayRemoveAll.add(b);
                                arrayRemoveAll.add(c);
                                arrayRemoveAll.add(d);
                                arrayRemoveAll.add(e);
                                arrayRemoveAll.add(f);
                                arrayRemoveAll.add(g);
                                arrayRemoveAll.add(h);

                                row.createCell(0).setCellValue(arrayExport.get(0));
                                row.createCell(1).setCellValue(arrayExport.get(1));
                                row.createCell(2).setCellValue(arrayExport.get(2));
                                row.createCell(3).setCellValue(arrayExport.get(3));
                                row.createCell(4).setCellValue(arrayExport.get(4));
                                row.createCell(5).setCellValue(arrayExport.get(5));
                                row.createCell(6).setCellValue(arrayExport.get(6));
                                row.createCell(7).setCellValue(arrayExport.get(7));

                            }
                        }
                    }

                    try {
                        if (!filePath.exists()) {
                            filePath.createNewFile();
                            Toast.makeText(TeacherDash.this, "Download success", Toast.LENGTH_SHORT).show();

                        }

                        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                        hssfWorkbook.write(fileOutputStream);

                        if (fileOutputStream != null) {
                            fileOutputStream.flush();
                            fileOutputStream.close();
                        }
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                }
            }
        });
    }
});

Advertisement

Answer

You are looping over things multiple times where you probably don’t need to be. If you want to get multiple documents from a collection and have each document be a single row in the spreadsheet where the document fields fill the columns within that row, then you only need a single loop – over documents. It would look something like this:

HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet = hssfWorkbook.createSheet(TeacherDash.subjectName1);

int rowNum = 0;
for (DocumentSnapshot documentSnapshot : task.getResult()){

    // Create a new row for each document
    HSSFRow row = hssfSheet.createRow(rowNum);
    ++rowNum;

    // Get the data from the firestore document 
    // you want to put in this row
    String name = documentSnapshot.getString("Name");
    String date = documentSnapshot.getString("Date");
    String time = documentSnapshot.getString("Time");
    String num  = documentSnapshot.getString("StudentNumber");
    String course = documentSnapshot.getString("Course");
    String sub = documentSnapshot.getString("Subject");
    String room = documentSnapshot.getString("Room");
    String sched = documentSnapshot.getString("Schedule");

    // Fill the contents of that row
    row.createCell(0).setCellValue(name);
    row.createCell(1).setCellValue(date);
    row.createCell(2).setCellValue(time);
    row.createCell(3).setCellValue(num);
    row.createCell(4).setCellValue(course);
    row.createCell(5).setCellValue(sub);
    row.createCell(6).setCellValue(room);
    row.createCell(7).setCellValue(sched);
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement