java - Firebase,从所有文档中获取数组列表字段

标签 java android firebase google-cloud-firestore

我需要查询一个集合..这个集合“Group”包含许多文档,显然每个文档都包含一个标题(id)、一个参与者数组(其中字段名为“partecipant”)和一个字段“numPartecipants”,每组的参与者总数。我附上数据库的照片: enter image description here

现在,我可以使用以下代码获取“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);

但是如果我知道该组的名称就可以了。 在本例中,我想从“Group”集合中提取每个文档,并从每个文档中提取 Id 和参与者数组。我想我不能使用 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 = [纽约、普罗瓦、测试] partecipantsArray = [Andrea、Tom、Spencer ........、Nico、Raul、Lorenzo、Eli]

最佳答案

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

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

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

关于java - Firebase,从所有文档中获取数组列表字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53747054/

相关文章:

java - "UCanAccess: user lacks privilege or object not found"第二个连接

android - 我可以在本地克隆 Parse.com 数据库以避免对数据库的高速率访问吗

android - 来自 admob 的广告需要显示多长时间才能算作展示

ios - 当从 Firebase 控制台更改数据时,如何在 Firebase 上执行多路径数据更新?

ios - 当应用程序不在后台时无法识别 Firebase 动态链接 (iOS)

javascript - 如何让 DOM 自动更新 - Firebase 问题

java - 为什么当我运行程序时,会显示代码中的所有字母等级选项?我只想显示正确答案

java - 使用 mFusedLocationClient 获取 Firebase 服务中的当前位置

java - 将 Browserfield.displayContent 与具有外部 url 的 <frame> 一起使用

android - 如何通过布局禁用谷歌地图触摸事件? (安卓)