亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Android 上的 Firebase Firestore 查詢對象列表

從 Android 上的 Firebase Firestore 查詢對象列表

哈士奇WWW 2021-08-06 10:02:31
我正在嘗試創建一種從我的Cloud Firestore database. 然后我會RecyclerView用上述列表設置一個適配器,它應該能很好地顯示。截止目前,RecyclerView空空如也。(我還RecyclerView用虛擬項目測試了它們,它們顯示得很好)在我的方法結束時,項目列表仍然是空的。日志以奇怪的順序顯示(最后一個日志顯示在其他日志之前)。日志順序讓我懷疑在偵聽器中運行了一些單獨的線程。我不確定如何同步它們。private List<Project> projects;private FirebaseFirestore db;...在onCreateView()(我正在處理一個片段): db = FirebaseFirestore.getInstance(); queryAllProjects(); recyclerView.setAdapter(new ProjectRecyclerViewAdapter(projects, ...... private void queryAllProjects() {        projects = new ArrayList<>();         //I've already tried to make a local list and return that, however,         //the compiler would force me to declare the list as final here, and it wouldn't solve anything.        db.collection("projects")                .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());                                Project project = document.toObject(Project.class);                                projects.add(project);                            }                            Log.d(TAG, "After for loop: " + projects.toString());                             //Here the list is OK. Filled with projects.                             //I'd like to save the state of the list from here                        } else {                            Log.d(TAG, "Error getting document: ", task.getException());                            Toast.makeText(getContext(), R.string.error, Toast.LENGTH_SHORT).show();                        }                    }                });這是我一直在關注的官方文檔https://firebase.google.com/docs/firestore/query-data/get-data#custom_objects
查看完整描述

2 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

projects.add(project);

adapter.notifyDataSetChange()

添加列表后調用 notifyDataSetChange()


查看完整回答
反對 回復 2021-08-06
  • 2 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號