Java 将对象添加到ArrayList

Java 将对象添加到ArrayList,java,android,firebase,arraylist,Java,Android,Firebase,Arraylist,我正在尝试将从Firebase数据库接收的对象添加到ArrayList。但是,即使在调用add方法之后,列表仍然保持为空。我做错了什么?我正在尝试从firebase加载数据,并将其显示在列表视图中。我使用日志语句来检查是否正在接收来自firebase的数据,并且它是正确的。有人有什么建议吗 public class RescueFragment extends Fragment { public RescueFragment() { // Required empty

我正在尝试将从Firebase数据库接收的对象添加到ArrayList。但是,即使在调用add方法之后,列表仍然保持为空。我做错了什么?我正在尝试从firebase加载数据,并将其显示在列表视图中。我使用日志语句来检查是否正在接收来自firebase的数据,并且它是正确的。有人有什么建议吗

public class RescueFragment extends Fragment {

    public RescueFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.rescue_list, container, false);

        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference ref = database.getReference("rescue");

        final ArrayList<RescueAnimal> rescueList = new ArrayList<>();

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
                    String location = (String) messageSnapshot.child("location").getValue();
                    String appearance = (String) messageSnapshot.child("appearance").getValue();
                    String photo = (String) messageSnapshot.child("photo").getValue();
                    String species = (String) messageSnapshot.child("species").getValue();
                    String problem = (String) messageSnapshot.child("problem").getValue();
                    rescueList.add(new RescueAnimal(location, appearance, photo, species, problem));
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        RescueAdapter adapter = new RescueAdapter(getActivity(), rescueList);
        ListView listView = view.findViewById(R.id.rescue_list);
        listView.setAdapter(adapter);
        return view;
public类救援片段扩展片段{
公共援助碎片(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.rescue\u列表,容器,错误);
final FirebaseDatabase=FirebaseDatabase.getInstance();
databasereferef=database.getReference(“rescue”);
最终ArrayList救援列表=新建ArrayList();
ref.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
对于(DataSnapshot messageSnapshot:DataSnapshot.getChildren()){
字符串位置=(字符串)messageSnapshot.child(“位置”).getValue();
字符串外观=(字符串)messageSnapshot.child(“外观”).getValue();
String photo=(String)messageSnapshot.child(“photo”).getValue();
String species=(String)messageSnapshot.child(“species”).getValue();
字符串问题=(字符串)messageSnapshot.child(“问题”).getValue();
救援名单。添加(新的救援动物(位置、外观、照片、物种、问题));
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
救援适配器=新的救援适配器(getActivity(),救援列表);
ListView ListView=view.findViewById(R.id.rescue\u列表);
setAdapter(适配器);
返回视图;

您得到的是
null
,因为您在
onDataChange()
之外声明了
rescueList
ArrayList。这是由于此方法的异步行为造成的。这意味着将这些对象添加到ArrayList的语句在
onDataChange()之前执行
方法已被调用。要解决此问题,您需要在
onDataChange()
内部声明并使用该
ArrayList
。小心不要在for循环内部添加


如果你想在该方法之外使用这些值,我建议你阅读我的答案。

列表在哪里使用?你怎么知道它是空的?你的
onDataChange
函数异步执行。因此你调用了。在使用列表后添加。在
救援列表之前添加打印语句。添加
RescueAdapter adapter=new-RescueAdapter(getActivity(),rescueList);
查看发生了什么。调试代码时是否在
for
循环中放置了断点,以确保它正在执行?还是日志语句?是否在调试模式下运行此操作?我使用了函数isEmpty()如果它是空的,则在日志中打印。而且,在片段中没有显示任何内容,因为适配器正在被馈送一个空的ArrayList。我正在使用notifyDataSetChanged(),但它在更新或初始化时相当慢。有没有更好的方法?您可以查看一下。所有事情都是实时发生的。