gpt4 book ai didi

java - 如何在返回语句之前从 FirebaseFirestore 获取所有数据

转载 作者:行者123 更新时间:2023-11-30 05:03:25 25 4
gpt4 key购买 nike

<分区>

我需要一个函数来从 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。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com