简体   繁体   English

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

[英]Firebase, get an arraylist field from all documents

I need to query a collection..this collection "Group" contains many documents, and each of them contains, obviously, a title(the id), an array of partecipants, with field named "partecipant", and a field "numPartecipants",with the total partecipants of each group. 我需要查询一个集合。该集合“组”包含许多文档,并且每个文档都包含一个标题(id),一组参与者数组,其字段名为“ partecipant”,以及一个字段“ numPartecipants” ,以及每组的总参加人数。 I attach a photo of the db: 我附上一张分贝的照片: 在此处输入图片说明

Now, I can get the "partecipant" field and save it into an array with this code: 现在,我可以使用以下代码获取“ partecipant”字段并将其保存到数组中:

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

But that's work if I know the name of the group. 但是,如果我知道小组的名称,那是可行的。 In this case, I want to extract from the "Group" collection each documents, and from each documents the Id and the partecipants array..I think I can't use Task, but I have to use QueryDocumentSnapshots. 在这种情况下,我想从“组”集合中提取每个文档,并从每个文档中提取Id和partecipants数组。.我认为我不能使用Task,但是必须使用QueryDocumentSnapshots。 That's my code. 那是我的代码。

    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)); }
                        }
                    }


            });


}

And the output is completely wrong: 输出完全错误: 在此处输入图片说明

The output instead should be: title = [New York, Prova, Test] partecipantsArray = [Andrea, Tom, Spencer ........ , Nico, Raul, Lorenzo, Eli] 输出应该改为:title = [New York,Prova,Test] partecipantsArray = [Andrea,Tom,Spencer ........,Nico,Raul,Lorenzo,Eli]

To get the array from within a single document, only these lines of code are required: 要从单个文档中获取数组,只需要以下几行代码:

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);
                }
            }
        }
    }
});

The output in your logcat will be: 您的logcat中的输出将是:

Nico
Raul
Lorenzo
Eli

If you want to get all arrays from within all documents, please use the following lines of code: 如果要从所有文档中获取所有数组,请使用以下代码行:

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);
                    }
                }
            }
        }
    }
});

The output will be all partecipants within all documents. 输出将是所有文档中的所有参与者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Elasticsearch从所有文档中获取字段的值 - Elasticsearch Get the values of a field from all the documents 获取集合中的所有文档后无法将字段添加到 ArrayList、Android Studio、Java、Firebase Firestore - After getting all documents in collection can't add field to ArrayList, Android Studio, Java, Firebase Firestore 如何从 Firebase 中的两个不同的 collections 获取所有文档? - How to get all documents from two different collections in Firebase? 在ArrayList中获取字段的所有含义 火力基地 - Getting all meanings of field in ArrayList | Firebase 如何从Firebase获取所有Childs的Java对象的ArrayList - How Can I get ArrayList of Java Objects of all Childs from Firebase 从 arraylist firebase 中随机获取 object - Get random object from arraylist firebase 如何从其他文档(ElasticSearch)中按字段值获取嵌套文档? - How to get nested documents by field value from other documents (ElasticSearch)? 从方法获取数组列表到全局字段 - Get arraylist from method into a global field 如何将子集合中的所有文档获取到 Firebase Firestore 中的 RecyclerView - How to get all documents in sub-collections to RecyclerView in Firebase Firestore 从 Couchbase 存储桶中获取所有文档 - Get all documents from Couchbase bucket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM