![](/img/trans.png)
[英]How to get specific list of documents from “Firestore” collection if i have users ID arraylist in “Android” using Query
[英]Java Firestore Android use an arraylist in a query to show posts from followed users
我正在開發一個顯示用戶關注的用戶帖子的活動。 對於帖子,我使用了以下結構:
root --- posts (collection)
|
--- uid (documents)
|
--- userPosts (collection)
|
--- postId (documents)
| |
| --- title: "Post Title"
| |
| --- date: 4/06/2021
|
--- postId (documents)
|
--- title: "Post Title"
|
--- date: 08/06/2021
|
--- description: "this is the description of the post"
[...etc]
最近我做了一個可視化用戶帖子的活動,就是這樣。 現在,相反,我想在主頁上顯示用戶關注的用戶的帖子。 這是“以下”的結構:帖子(集合)
root--- folowing(documents)
|
--- uid(collection)
|
--- userFollowing(documents)
|
--- followed user(collection) -- followed user(document)
|
--- followed user (collection) -- followed user(document)
因此,我嘗試在Arraylist上收集用戶以這種方式遵循的所有用戶 uid:
FirebaseFirestore.getInstance().collection("following").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("userFollowing").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
document.getData();
Uidrecord.add(cont,String.valueOf(document.getData()));
cont++;
}
int conta = 0;
for (String item: Uidrecord){
if(item == null)
break;
uidFollowing.add(conta,getFollowingUid(item));
Toast.makeText(MainActivity.this, uidFollowing.get(conta), Toast.LENGTH_SHORT).show();
conta++;
}
} else {
Toast.makeText(MainActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
}
}
});
(uid 以下包含用戶的所有 id)這是我設置查詢以獲取單個用戶的帖子數據的方式:
Query query = FirebaseFirestore.getInstance()
.collection("post/" + uidVisit + "/userPosts")
.limit(1000);
我知道我必須在其中查找帖子的用戶 ID,所以這是一個 static 查詢,因為我必須獲取用戶的帖子,僅此而已。 現在,獲取帖子的 uid 包含在 arraylist 中。 所以我想知道如何創建一個動態查詢,然后如何獲取每個關注的用戶的帖子(使用 RecyclerView)。 有可能做到嗎?
編輯:正如您從我的完整代碼https://codeshare.io/MNWYb3中看到的那樣,我嘗試放置一個 for(第 225 行)並動態化查詢,但結果是它只顯示用戶的帖子,僅此而已(似乎覆蓋了每個適配器,實際上顯示的帖子是最后一個人,所以最后一個 arraylist 項目)
以您當前在 Firestore 上的結構,這將有點困難,我認為您應該首先簡化您的結構,我建議您保留這樣的結構:
users
|
--- uid : "uid1"
name: "John Doe"
...
usersFollowed : [uid1, uid2, ..., uidn]
userPosts (collection)
|
--- uid: "uuid1"
title: "Post Title"
date: 08/06/2021
description: "this is the description of the post"
userId: "uid1"
....
有了這個,您只對單個集合和它的子集合進行操作,這會稍微降低復雜性,您會將每個用戶關注的用戶列表存儲在每個用戶文檔的usersFollowed
數組字段中。
接下來,您可以隨機迭代usersFollowed
列表:
List<String> listOfFollowed = new ArrayList<>();
// userDocument is your current user
listOfFollowed = userDocument.get(usersFollowed);
Collections.shuffle(listOfFollowed);
List<DocumentSnaphot> listOfPosts = new ArrayList<DocumentSnaphot>();
for (String item : listOfFollowed) {
Query query = FirebaseFirestore.getInstance()
.collection("users")
.document(userDocument.getId())
.collection("userPosts")
.document(item)
.orderBy("date")
.limit(1);
listOfPosts.add(query.get().getDocument().get(0));
}
此代碼將為您提供每個用戶的單個最新帖子,當前用戶以隨機順序跟隨該帖子。
這不是一個動態查詢,但它會給你隨機結果,你可以調整你想首先從每個用戶那里獲得多少帖子背后的邏輯,或者創建一個 function 被觸發並在用戶發布時獲得更多帖子看到所有你已經拿到的等等。
編輯
檢查您的完整代碼我得出的結論是,您面臨的問題與FirestoreRecyclerAdapter
的使用有關,它需要查詢作為參數,但是,您的用例要求您在循環中一遍又一遍地執行查詢得到結果。 將相同的查詢作為FirestoreRecyclerAdapter
的選項傳遞將導致您獲得一個僅獲取列表中最后一個userFollowed
的查詢,因為這將是循環中的最后一次迭代,並且將是應用程序執行中向前移動的內容,這不是預期的結果。
因此,我建議您使用ListAdapter
,而不是使用FirestoreRecyclerAdapter
,它需要一個 List 作為參數。 您將用作此參數的列表將是一個新列表,其中填充了在您創建的循環中一遍又一遍地執行的查詢結果。
話雖如此,我建議您查看該文檔並查看這可能在您的代碼中暗示的其他更改,因為您將更改您正在使用的 class 的類型,這可能會產生意想不到的后果。 本教程也可能會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.