【发布时间】:2021-05-29 13:56:50
【问题描述】:
这是我的代码 我在我的适配器中使用这个数组列表在列表视图中显示
public ArrayList<String> loadFromFirebase() {
ArrayList<String> arrayList = new ArrayList<>();
db.collection("notite").whereEqualTo("User email", user.getEmail())
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful() && !task.getResult().isEmpty()) {
DocumentSnapshot documentSnapshot = task.getResult().getDocuments().get(0);
String documentId = documentSnapshot.getId();
db.collection("notite").document(documentId).get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.exists()) {
String notita = documentSnapshot.getString("Detalii");
arrayList.add(notita);
} else {
Toast.makeText(getContext(), "Notes does not exist",
Toast.LENGTH_LONG).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), "Failure retrieving",
Toast.LENGTH_LONG).show();
Log.d("Retrieve", e.toString());
}
});
} else {
Toast.makeText(getContext(), "Does not exist"
, Toast.LENGTH_LONG).show();
}
}
});
就在这里
return arrayList;
}
在返回之前,我的arraylist 是空的。你们能帮帮我吗?
【问题讨论】:
-
Firebase API 是异步的。所以,这很可能是因为this。
-
@AlexMamo 这解决了我的新问题。非常感谢!
标签: java android firebase google-cloud-firestore