andrid中的ArrayList在从firebase数据库获取数据时不返回数据



我试图从firebase数据库中获取记录,并返回数组列表中的所有记录,但在for循环中,它会添加记录,但当我们在循环外检查时,它不包含记录。有人能帮我吗?我是安卓系统的新手。这是获取数据的代码。。

public class DataBaseManagement {
public static ArrayList<Property> getAllProperties(Context context, DatabaseReference dbProperty){

ArrayList<Property> propertyList = new ArrayList<Property>();
dbProperty.get().addOnSuccessListener(new OnSuccessListener<DataSnapshot>() {
@Override
public void onSuccess(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
Property tempProp;
for (DataSnapshot aProp: dataSnapshot.getChildren())
{
tempProp = aProp.getValue(Property.class);
propertyList.add(tempProp);
}
Toast.makeText(context, "Total Properties : " + propertyList.size(), Toast.LENGTH_SHORT).show(); // Here it will show the number of records
}
else {
Toast.makeText(context, "No record found", Toast.LENGTH_SHORT).show();
}
}
});
Toast.makeText(context, "Total Properties : " + propertyList.size(), Toast.LENGTH_SHORT).show(); // here it will not show the number of records
return propertyList;
}

}

正如我在addOnSuccessListener中所知,它在后台线程中工作fun将返回并完成后台线程,我想如果你想的话,可以将return fun更改为void并使用fun liveData并在addOnSuccessListener 中更新它

最新更新