【发布时间】:2018-06-03 09:47:50
【问题描述】:
我正在使用Google Cloud Firestore 来存储我的应用程序的数据,并且我正在尝试检索该数据并填充ArrayList。
ArrayList 是全局声明的,并在匿名内部类中成功更新,但是当我在调用包含匿名内部类的方法后使用 ArrayList 时(以及使用最下面的 Log 测试其内容) call),它显示为空。
我一直在努力解决这个问题 - 任何帮助都将不胜感激!
谢谢。
@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);
}
【问题讨论】:
标签: java android class arraylist anonymous-class