我需要一个函数来从 FirebaseFirestore 获取所有数据/注释。如何让这个函数在返回之前等待所有数据?
我认为我创建的这个函数在主线程中不起作用,并且在从 firebase 获取数据之前返回
public static ArrayList<NoteFB> getNotes() {
FirebaseFirestore db = FirebaseFirestore.getInstance();
final String TAG = "FB Adapter";
final ArrayList<NoteFB> doFBs = new ArrayList<>();
db.collection("notesItem")
.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());
doFBs.add(document.toObject(NoteFB.class));
}
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
return doFBs;
}
MyFBAdapter myFBAdapter = new MyFBAdapter(ShowActivity.this, FBAdapter.getNotes());
rvContacts.setAdapter(myFBAdapter);
此代码返回一个空的 ArrayList,它创建一个空的 recyclerview。
叮当猫咪
烙印99
相关分类