我有一个recyclerView,直接从onCreate调用时加载正常 . 但是,当将其插入Firestore Collection的onSuccessListener时,它不会加载 . 以下是所述代码的示例:
private void populateRecycler(String RootCollection, String UserID) {
mQuery = mFirestore
.collection(RootCollection)
.whereEqualTo("UID", UserID)
.orderBy("DateAdded", Query.Direction.DESCENDING);
mAdapter = new OrdersAdapter(mQuery, this) {
@Override
protected void onDataChanged() {
if (getItemCount() == 0) {
mOrdersRecycler.setVisibility(View.GONE);
mEmptyView.setVisibility(View.VISIBLE);
} else {
mOrdersRecycler.setVisibility(View.VISIBLE);
mEmptyView.setVisibility(View.GONE);
}
}
protected void onError (FirebaseFirestoreSettings e) {
// Show a snackbar on errors
Snackbar.make(findViewById(android.R.id.content),
"Error: check logs for info.", Snackbar.LENGTH_LONG).show();
}
};
// Populate and enable RecyclerView
mOrdersRecycler.setLayoutManager(new LinearLayoutManager(this));
mOrdersRecycler.setAdapter(mAdapter);
}
但是当我从另一个包含此代码的方法调用 populateRecycler();
时:
mFirestore.collection("UIDOrg")
.document(UserID)
.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
UIDOrg uidOrg = documentSnapshot.toObject(UIDOrg.class);
mOrgTextView.setText(uidOrg.getOrg());
RootCollection = uidOrg.getRootCollection();
populateRecycler(RootCollection, UserID);
}
...这是行不通的 . 有人可以解释一下原因吗?
1 回答
在您的
populateRecycler
方法中是异步操作 . 这意味着将来可以获得结果 . 因此,您需要在填充数据之前等待结果 .阅读这个概念chaining of the tasks . 在完成第二项任务后,再填充您的回收站视图 .