簡體   English   中英

如果我使用Query在“Android”中擁有用戶ID arraylist,如何從“Firestore”集合中獲取特定的文檔列表

[英]How to get specific list of documents from “Firestore” collection if i have users ID arraylist in “Android” using Query

實際上我正在我的firestore文檔下存儲地圖對象和一些用戶ID,我這樣阻止。 點擊下面的鏈接查看圖片

我有我在社交應用程序中阻止的所有成員ID,我需要使用firestore查詢獲取所有這些成員文檔,但如果arraylist有多個索引,則查詢不起作用。但是當我只有一個id時,查詢工作完美。 這是代碼

 private ArrayList<String> blocks =new ArrayList<>();
    if (logedInMember != null){
        blocks = logedInMember.getBlocks();
        if (!blocks.isEmpty()){
            Query query;
            CollectionReference collection = firestore.collection(Constants.MEMBERS);
            query  = collection;

            for (int i = 0 ; i< blocks.size(); i++){
                Log.d("block member ID :", blocks.get(i) );
                // now its time to query all these ids
                String id = blocks.get(i);
                if (!id.isEmpty()){
                    query = query.whereEqualTo(Constants.ID,id);
                }
            }

            query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()){


                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Member member = document.toObject(Member.class);
                            Log.d("Member Id :", member.getId());
                            Log.d("Member Name :", member.getName());

                        }
                    }else {
                       // loader.dismissProgress();
                        Log.d("error : ","fail to load query");
                    }
                }
            });
}
}

如果arraylist有多個索引,則查詢不起作用。但是當我只有一個id時,查詢工作正常

嘗試這個

// RETRIEVING ALL Queries
Query allQueries = firebaseFirestore
                    .collection("yourcollection")                    
                    .orderBy("timestamp", Query.Direction.DESCENDING)
                    .limit(5);

            allQueries.addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {


                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            // get all ids
                            String postId = doc.getDocument().getId();
                            contentProfileDashboard = doc.getDocument().toObject(ContentProfileDashboard.class);                                                             
                                                     contentListDashboard.add(contentProfileDashboard);                         

                            // fire the event           
                            adapterProfileDashboard.notifyDataSetChanged();
                        }
                    }


                }
            });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM