首页 > 解决方案 > 从 Firestore 获取 ArrayList 和文档名称

问题描述

我是 Google Firebase 的新手,我正在尝试了解它。我正在做一个 Android 应用程序,您可以在其中创建一组人员并设置组的标题。然后,在“组页面”中,您可以在列表视图中看到您的所有组。我的 Firestore 数据库的结构是这样的:

users --> email(document) ---> Group(collection) --> GroupName(Document)并且组名文档包含参与者数组列表(参与者 0:Name1,partecipant1:name2 等)。

我想检索文档ID(这是组标题)和参与者的arrayList,但我不知道在代码中使用for each ...

这是我的代码:

public void load_list_view(){

    String email = getEmail();
    final DocumentReference docRef = db.collection("users").document(email).collection("Group").document();

    docRef.get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {


                        titleArray.add(documentSnapshot.getId());
                        titleString = documentSnapshot.getId();

                        partecipantsArray.add(documentSnapshot.getString("partecipant"));
                        num_partecipants =  partecipantsArray.size();
                        numArray.add(num_partecipants);
                        trash = R.drawable.trash_icon;
                        firstChar = Character.toString(titleString.charAt(0));
                        firstCharArray.add(firstChar);
                        customAdapter = new GroupAdapter(GroupActivity.this, firstCharArray, titleArray, numArray, trash);
                        listView.setAdapter(customAdapter);


                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(GroupActivity.this, e.getStackTrace().toString(), Toast.LENGTH_LONG).show();

                }
            });

}

titleArray.add(documentSnapshot.getId());它检索一个随机 ID,我不明白为什么。

我在 Internet 上没有找到足够的关于Arraylist和 firestore 的文档。

标签: javaandroidfirebasegoogle-cloud-firestore

解决方案


首先,要获取集合中的所有文档,您应该按照本文档中所示的方式编写不同的代码。

db.collection("cities")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Log.d(TAG, document.getId() + " => " + document.getData());
                }
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

其次,如果您正在检索一个 ArrayList,您应该使用(ArrayList<String>) documentSnapshot.get("key")而不是documentSnapshot.getString("key").


第三,您将获得随机 ID,因为使用这行代码(如下所述),firebase 正在生成一个带有随机 ID 的新文档引用。参考链接

    final DocumentReference docRef = db.collection("users").document(email).collection("Group").document();

为了您的帮助,我已经调整了您的代码,您可以尝试此代码并检查它是否正常工作。

public void load_list_view() {

        String email = getEmail();
        final DocumentReference docRef = firestore.collection("users").document(email);

        docRef.collection("Group")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        for (QueryDocumentSnapshot document : queryDocumentSnapshots) {

                            //Extracting Group name from each document

                            titleString = document.getId();
                            titleArray.add(titleString);

                            //Extracting participants ArrayList from each document

                            partecipantsArray.add((ArrayList<String>) document.get("participant"));
                            numArray.add(num_partecipants);
                            firstChar = Character.toString(titleString.charAt(0));
                            firstCharArray.add(firstChar);

                        }

                        num_partecipants = partecipantsArray.size();
                        numArray.add(num_partecipants);
                        trash = R.drawable.trash_icon;
                        firstChar = Character.toString(titleString.charAt(0));
                        firstCharArray.add(firstChar);
                        customAdapter = new GroupAdapter(GroupActivity.this, firstCharArray, titleArray, numArray, trash);
                        listView.setAdapter(customAdapter);

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        //HANDLE EXCEPTION
                    }
                });
    }

推荐阅读