我可以在for循环中读取数据,但是当我在所有情况下返回列表时,都返回null。请帮助我从函数获取列表回调。谢谢大家!
List<TableModel> list;
public List<TableModel> getAllTable()
{
list = new ArrayList<>();
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
DataSnapshot dataSnapshot = snapshot.child("Ban");
for (DataSnapshot valueTable : dataSnapshot.getChildren()) {
TableModel tableModel = valueTable.getValue(TableModel.class);
Log.d("checktenban",tableModel.getTenBan());
list.add(tableModel);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
};
nodeRoot.addListenerForSingleValueEvent(valueEventListener);
Log.d("size of list", String.valueOf(list.size()));
return list;
}
}