我目前正在尝试使用 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/