gpt4 book ai didi

java - 如何解决 Android Studio Firebase 问题?

转载 作者:行者123 更新时间:2023-12-01 16:21:48 32 4
gpt4 key购买 nike

我正在尝试从 Firebase 获取数据到 bilgiler 变量。但我总是出错。我该如何解决这个问题?

Map<String, String> bilgiler = new HashMap<>();

db.collection("tarihBilgi")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
//Log.d("TAG", document.getId() + " => " + document.getData());
bilgiler.put("asdf", document.getData().toString());
}
} else {
Log.w("TAG", "Error getting documents.", task.getException());
}
}
});
Log.i("asdfads",(bilgiler.get("asdf")));

最佳答案

数据是从 Firebase 异步加载的。现在你的Log.i("asdfads",(bilgiler.get("asdf")));bilgiler.put("asdf", document.getData().toString())之前运行运行,这解释了为什么您看不到数据库中的值。

任何需要数据库数据的代码都需要位于 onComplete 内方法,或者从那里调用。所以:

db.collection("tarihBilgi")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
bilgiler.put("asdf", document.getData().toString());
Log.i("asdfads",(bilgiler.get("asdf")));
}
} else {
Log.w("TAG", "Error getting documents.", task.getException());
}
}
});

另请参阅:

关于java - 如何解决 Android Studio Firebase 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62251782/

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