我有一个应用程序在构建过程中,我需要从 FirebaseDatabase 获取数据并在自定义列表视图中显示它们在这里我的代码部分用于 onDataChange 方法
myDatabase=FirebaseDatabase.getInstance();
myRef= myDatabase.getReference().child("TvSeries");
myAuth = FirebaseAuth.getInstance();
myUser = myAuth.getCurrentUser();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot currentData : dataSnapshot.getChildren()){
if(currentData.child("tCategory").child("tPrimary").getValue().toString().equals("Aksiyon")){ }
selectedCategoryList.add(new DataForRow(currentData.getKey(),
currentData.child("tCategory").child("tPrimary").getValue().toString(),
currentData.child("tReleaseDate").getValue().toString()));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Integer size =selectedCategoryList.size();
Log.d("Size:", size.toString());
当我在 onDataChange 方法中设置断点时,它可以工作,但没有任何建议非常有帮助。祝大家有个美好的一天。
您的selectedCategoryList
列表始终为空,因为onDataChange()
方法具有异步行为,这意味着即使在您尝试将这些DataForRow
类对象添加到列表之前,它也会被调用。快速解决此问题的方法是selectedCategoryList
仅在onDataChange()
方法内部声明和使用您的列表,或者如果您想在外部使用它,则需要创建自己的回调,为此,我建议您从这里查看我的答案的最后一部分发布。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句