我正在嘗試創(chuàng)建一種從我的Cloud Firestore database. 然后我會(huì)RecyclerView用上述列表設(shè)置一個(gè)適配器,它應(yīng)該能很好地顯示。截止目前,RecyclerView空空如也。(我還RecyclerView用虛擬項(xiàng)目測(cè)試了它們,它們顯示得很好)在我的方法結(jié)束時(shí),項(xiàng)目列表仍然是空的。日志以奇怪的順序顯示(最后一個(gè)日志顯示在其他日志之前)。日志順序讓我懷疑在偵聽(tīng)器中運(yùn)行了一些單獨(dú)的線程。我不確定如何同步它們。private List<Project> projects;private FirebaseFirestore db;...在onCreateView()(我正在處理一個(gè)片段): 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(); } } });這是我一直在關(guān)注的官方文檔https://firebase.google.com/docs/firestore/query-data/get-data#custom_objects
2 回答

猛跑小豬
TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
projects.add(project);
adapter.notifyDataSetChange()
添加列表后調(diào)用 notifyDataSetChange()
添加回答
舉報(bào)
0/150
提交
取消