如何从集合中获取所有数据

时间:2021-02-17 08:05:58

标签: java android

我正在尝试从第一个文档中获取所有数据并移至下一个文档并执行相同操作。

CollectionReference fstore = fs.collection("Diagnostics").document(email).collection("Date");
fstore.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot documentSnapshot) {
        if(!documentSnapshot.isEmpty()){
            for (DocumentSnapshot documentSnapshots : documentSnapshot) {
                String date = documentSnapshots.getId();
                String faultcodes = documentSnapshots.getString("Code");
                String description = documentSnapshots.getString("Description");
                TroubleCodes pastsc = new TroubleCodes(date,faultcodes,description);
                pastscan.add(pastsc);
                pastscansdpt.setAdapter(new PastScanAdapter(pastscan, PastScanpage.this));

            }
        }
        else{
            TroubleCodes nosc = new TroubleCodes("No Diagnosis Scans",null, null);
            pastscan.add(nosc);
            pastscansdpt.setAdapter(new PastScanAdapter(pastscan, PastScanpage.this));
        }
    }
});

1 个答案:

答案 0 :(得分:0)

不要为每个文档一遍又一遍地为 RecyclerViewListView 创建和设置适配器,而是创建您的适配器并将其设置为 RecyclerViewListView 并使用空过去扫描只列出一次。然后获取您的数据,添加到列表中并通知适配器。

   pastscansdpt.setAdapter(new PastScanAdapter(pastscan, PastScanpage.this));
    CollectionReference fstore = fs.collection("Diagnostics/" + email + "/Date");
    fstore.get().addOnSuccessListener(queryDocumentSnapshots -> {
        //Clear your list otherwise whenever this method triggers it'll add same documents over and over again.
        pastscan.clear();
        if (!queryDocumentSnapshots.isEmpty()) {
            for (DocumentSnapshot document : queryDocumentSnapshots) {
                if (document.exists()) {
                    //According to your codes your document data structure is same as TroubleCodes.
                    //So you don't need to get all fields one by one.
                    TroubleCodes pastsc = document.toObject(TroubleCodes.class);
                    //Add each document in to your list.
                    pastscan.add(item);
                }
            }
        } else {
            TroubleCodes nosc = new TroubleCodes("No Diagnosis Scans", null, null);
            pastscan.add(nosc);
        }
        //Notify your adapter.
        adapter.notifyDataSetChanged();
    });