本文介绍了使用addListenerForSingleValueEvent侦听器方法后,数组列表将为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Firebase数据库中获取数据到recyclerview.由于Array列表在addListenerForSingleValueEvent方法之后变为空,因此我无法将数据获取到RecyclerViewAdapter.

I am fetching data from firebase database to a recyclerview. I can't get the data to the RecyclerViewAdapter since the Array list becomes empty after the addListenerForSingleValueEvent method.

@Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        database = FirebaseDatabase.getInstance();
        ArrayList<Item> arr = new ArrayList<Item>();


        DatabaseReference reference = database.getReference("NoticeBoard");

        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {

                    String text1 = (String) singleSnapshot.child("Text1").getValue();
                    String text2 = (String) singleSnapshot.child("Text2").getValue();

                    arr.add(new Item(text1, text2));
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });

        RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), arr);// Here the ArrayList arr is empty

        RecyclerView recyclerView = view.findViewById(R.id.dashboardRecyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

        return view;
    }

推荐答案

onDataChange()是异步的,因此由于您在 addListenerForSingleValueEvent 之后调用arraylist,它将返回一个空列表,因为尚未检索数据.

onDataChange() is asynchronous therefore since you are calling the arraylist after addListenerForSingleValueEvent, it will return an empty list because the data is not yet retrieved.

要解决您的问题,您需要在 onDatachange()内添加arrayList:

To solve your problem, you need to add the arrayList inside onDatachange():

        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {

                    String text1 = (String) singleSnapshot.child("Text1").getValue();
                    String text2 = (String) singleSnapshot.child("Text2").getValue();
                    ArrayList<Item> arr = new ArrayList<Item>();
                    arr.add(new Item(text1, text2));

                    RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), arr);
                    RecyclerView recyclerView = view.findViewById(R.id.dashboardRecyclerView);

                    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                    recyclerView.setAdapter(adapter);
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });

这篇关于使用addListenerForSingleValueEvent侦听器方法后,数组列表将为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:34