Firebase,从所有文档中获取arraylist字段

时间:2018-12-12 16:11:33

标签: java android firebase google-cloud-firestore

我需要查询一个集合。该集合“组”包含许多文档,并且每个文档都包含一个标题(id),一组参与者数组,其中字段名为“ partecipant”,以及一个字段“ numPartecipants”,以及每个组的总参与者。我附上一张分贝的照片: enter image description here

现在,我可以使用以下代码获取“参与者”字段并将其保存到数组中:

public void getPartecipantsList(){
    String email = getEmail();
    final String groupTitle = getTitleBar();
    DocumentReference docRef = db.collection("users").document(email).collection("Group").document(groupTitle);

    docRef.get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {

                        DocumentSnapshot document = task.getResult();

                        //Extracting participants ArrayList from the document
                        for(Object item : task.getResult().getData().values()) {

                            String[] values = String.valueOf(item).replace("[", "").replace("]", "").replace(" ", "").split(",");

                            for (String value : values){
                                    partecipantsArrayList.add(value);
                            }

                        }
                        partecipantsArrayList.remove(String.valueOf("["));
                        partecipantsArrayList.remove(partecipantsArrayList.size() - 1);

但是,如果我知道小组的名称,那就可以了。 在这种情况下,我想从“组”集合中提取每个文档,并从每个文档中提取Id和partecipants数组。.我认为我不能使用Task,但是必须使用QueryDocumentSnapshots。 那是我的代码。

    public void getPartecipantsList() {
    final ArrayList<String> title = new ArrayList<>();
    String email = getEmail();
    DocumentReference docRef = db.collection("users").document(email);

    docRef.collection("Group")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            titleString = document.getId();
                            title.add(titleString);

                            String[] values = String.valueOf(document).replace("[", "").replace("]", "").replace(" ", "").split(",");

                                for (String value : values) {
                                    partecipantsArray.add(value);
                                }
                            }
                            partecipantsArray.remove(String.valueOf("["));
                            partecipantsArray.remove(partecipantsArray.size() - 1);

                            SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            for (int i = 0; i < title.size(); i++) {
                                editor.putInt(title.get(i) + "_size", partecipantsArray.size());
                            }
                            for (int i=0;i<title.size();i++) {
                                Log.v("Array", partecipantsArray.toString());
                                Log.v(title.get(i)+"array", String.valueOf(partecipantsArray.size()));
                            }
                            for (int i = 0; i < partecipantsArray.size(); i++) {
                                editor.putString(titleString + "_name" + i, partecipantsArray.get(i)); }
                        }
                    }


            });


}

输出完全错误: enter image description here

输出应为: title = [纽约,Prova,测试] partecipantsArray = [安德里亚,汤姆,斯宾塞........,尼科,劳尔,洛伦佐,以利]

1 个答案:

答案 0 :(得分:1)

要从单个文档中获取数组,只需要以下几行代码:

db.collection("users").document(email).collection("Group").document(groupTitle).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                ArrayList<String> arrayList = (ArrayList<String>) document.get("partecipant");
                //Do what you need to do with your ArrayList
                for (String s : arrayList) {
                    Log.d(TAG, s);
                }
            }
        }
    }
});

您的logcat中的输出将是:

Nico
Raul
Lorenzo
Eli

如果要从所有文档中获取所有数组,请使用以下代码行:

db.collection("users").document(email).collection("Group").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                if (document.exists()) {
                    ArrayList<String> arrayList = (ArrayList<String>) document.get("partecipant");
                    //Do what you need to do with your ArrayList
                    for (String s : arrayList) {
                        Log.d(TAG, s);
                    }
                }
            }
        }
    }
});

输出将是所有文档中的所有参与者。