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

查看:42
本文介绍了将对象添加到 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.这是由于此方法的异步行为而发生的.这意味着将这些对象添加到 ArrayList 的语句是在调用 onDataChange() 方法之前执行的.要解决这个问题,您需要在 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.

如果您想在该方法之外使用这些值,我建议您从这个 post.

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

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

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