【问题标题】:Getting an ArrayList from Firestore and the document name从 Firestore 获取 ArrayList 和文档名称
【发布时间】:2019-04-24 15:23:43
【问题描述】:

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

用户 --> 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 的文档。

【问题讨论】:

  • 请添加您的数据库结构以便更清楚地看到它。

标签: java android firebase google-cloud-firestore


【解决方案1】:

首先,要获取集合中的所有文档,您应该编写不同的代码,如this documentation 所示。

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&lt;String&gt;) documentSnapshot.get("key") 而不是 documentSnapshot.getString("key")


第三,您获得了随机 ID,因为使用这行代码(如下所述),firebase 正在生成一个具有随机 ID 的新文档引用。 Reference Link.

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多