我使用Google Cloud Firestore
存储应用程序的数据,我正在尝试检索该数据并填充ArrayList
。
ArrayList是全局声明的,并且在匿名内部类中成功更新,但是当我在调用包含匿名内部类的方法(以及使用以下内容测试其内容)之后使用ArrayList
时大多数日志调用),它显示为空。
我一直在把头发拉出来 - 任何帮助都会受到赞赏!
感谢。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mCostsArrayList = new ArrayList<>();
//Get costs and sales data from Cloud FireStore - updates mCostsArrayList & mSalesArrayList
getCostTransactions(costsCollectionRef);
//Gets Cloud Firestore costs transactions and puts to mCostsArrayList
private void getCostTransactions(CollectionReference collectionReference){
collectionReference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
if (documentSnapshots.isEmpty()){
Log.d(TAG, "OnSuccess: LIST EMPTY");
return;
} else {
//Convert the whole Query Snapshot to a list of objects
//Do not need to fetch each document
List<BandTransaction> costs = documentSnapshots.toObjects(BandTransaction.class);
mCostsArrayList.addAll(costs);
Log.d(TAG, "onSuccess" + mCostsArrayList);
return;
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getActivity(), "Error getting data", Toast.LENGTH_SHORT).show();
}
});
Log.d(TAG, "AFTER METHOD" + mCostsArrayList);
}
答案 0 :(得分:0)
您确定在Log.d(TAG, "AFTER METHOD" + mCostsArrayList);
方法之后调用了行onSuccess(QuerySnapshot documentSnapshots)
吗?这是一个回调方法,可以在以后调用它。尝试在2秒后拨打Log.d(...)
。
new Handler().postDelayed(
() -> Log.d(TAG, "AFTER METHOD" + mCostsArrayList),
2000);