将对象添加到ArrayList [英] Adding object to ArrayList

查看:216
本文介绍了将对象添加到ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add objects received from Firebase Database to an ArrayList. However, the list stays empty even after calling the add method. What am I doing wrong? I'm trying to load data from firebase and is display it in a list view. I used log statements to check if the data from firebase is being received and it is. Anybody with suggestions?

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;

推荐答案

之所以得到null,是因为您在onDataChange()之外声明了rescueList ArrayList.这是由于此方法的异步行为而发生的.这意味着将在onDataChange()方法被调用之前执行将这些对象添加到ArrayList的语句.要解决此问题,您需要在onDataChange()中声明并使用该ArrayList.注意不要在for循环中添加.

You are getting null because you are declaring the rescueList ArrayList outside the onDataChange(). This is happening due the asynchronous behaviour of this method. This means that the statement that adds those objects to the ArrayList is executed before onDataChange() method has been called. To solve this you need to declare and use that ArrayList inside onDataChange(). Be careful not to add inside the for loop.

如果您想在该方法之外使用这些值,则建议您从

If you want to use those values outside that method, i sugget you readning my answer from this post.

这篇关于将对象添加到ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆