java - Firestore 好友列表未显示正确的方式

标签 java android google-cloud-firestore

我目前正在尝试使用 Firestore 创建好友列表。我的编码可以工作,但方式不正确。 所以我的数据库中有一个 friend 列表,如下所示:Databse pic

因此文档保存在我的 ID 下,其中保存了接受好友请求的用户的其他 ID。

所以现在它在应用程序中看起来像这样:new database picture

它显示我有 3 个 friend ,这是正确的,当我单击其中一个个人资料时,它会向我显示我的 friend 的正确方式。

它应该是这样的:here

我的重要代码现在看起来像这样:

   mFirestore.collection("users").document(usid).collection("friends").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

            if (e != null) {

            }
            for(DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                if (doc.getType() == DocumentChange.Type.ADDED){

                    String user_id = doc.getDocument().getId();
                    //Toast.makeText(yourFriends.this, fAuth.getUid(), Toast.LENGTH_SHORT).show();
                    System.out.println(user_id);
                    System.out.println(fAuth.getUid());
                    if (fAuth.getUid().equals(user_id)){

                        Map map = doc.getDocument().getData();
                        System.out.println(map.size());
                        //Toast.makeText(yourFriends.this, map.size(), Toast.LENGTH_SHORT).show();


                        for(Object friendid : map.values()){

                            friendid.toString();


                            Users users = doc.getDocument().toObject(Users.class).withId((String)friendid);
                            //Toast.makeText(yourFriends.this, (String)friendid, Toast.LENGTH_SHORT).show();
                            usersList.add(users);
                        }
                    }
                    usersListAdapter.notifyDataSetChanged();

                }
            }
        }
    });

所以我正在获取我的 friend 列表,但它不会加载我的用户列表中的信息。

最佳答案

您的数据库结构错误。您必须在一个用户下再启动一个集合,并且在该集合中您需要存储文档网,并在该集合下存储 friend 数据。之后将该集合获取到 Listview 或 Recyclerview 它就会出现。

关于java - Firestore 好友列表未显示正确的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60873079/

相关文章:

java - 如何使用 Java 文件中的 R.Color 在 Android Studio 中更改操作栏 setBackgrounddrawable 颜色?

Java的Thread.sleep导致我之前调用的方法取消

android - fragment 不与 fragment 管理器相关联

java - 从 Android 月份重新排列 ArrayList

node.js - 如何根据firestore文档的时间戳属性触发云函数

firebase - 我无法从 firestore "Instance of ' Future<dynamic >'"获取数据

java - maven 安装仅在第二次尝试后抛出 "error in opening zip file"

android - Android 上的 Flutter Cloud Firestore 崩溃 - 空对象引用上的 Mutable Document.getKey()

firebase - Flutter/Firebase - 类型 'Future<int>' 不是类型 'int' 的子类型

java - SpringDataMongo MongoRepository 可分页未使用地理空间方法排序