首页 > 解决方案 > 使用 addListenerForSingleValueEvent 初始加载数据,使用 addChildEventListener 更新列表

问题描述

我对其中两个听众有意见。下面是场景。当我第一次加载页面时,为了防止 addChildEventListener 的“onChildAdded”在 addListenerForSingleValueEvent 的“onDataChange”之前被触发,我实现了以下代码。

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState)
{
    //List object initialization
    itemList1 = new ArrayList<>();
    itemList2 = new ArrayList<>();
    check = false;

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_nested_recycler_view, container, false);
    recyclerView = view.findViewById(R.id.nestedRecyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    readData(new FirebaseCallback()
    {
        @Override
        public void onCallBack(List<DonationRequestDetailsModel> list1, List<DonationRequestDetailsModel> list2)
        {
            if (list1.size() == 0)
            {
                confirmationAdapter1 = new ConfirmationAdapter(list2, item, location, email, getActivity());
                recyclerView.setAdapter(confirmationAdapter1);
                check = true;
            }
            else if(list1.size() == 1)
            {
                confirmationAdapter2 = new ConfirmationAdapter(list1, item, location, email, getActivity());
                recyclerView.setAdapter(confirmationAdapter2);
                check = true;
            }

        }
    });
    return view;
}

private interface FirebaseCallback
{
    void onCallBack(List<DonationRequestDetailsModel> list1, List<DonationRequestDetailsModel> list2);
}

private void readData(final FirebaseCallback firebaseCallback)
{
    //Loop through data in Firebase Database based on corresponding email and add data into List
    reference = FirebaseDatabase.getInstance().getReference();
    xreference = reference.child("Benefactor Request");
    xreference.addListenerForSingleValueEvent(new ValueEventListener()
    {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {
            for (DataSnapshot snapshot : dataSnapshot.getChildren())
            {
                if (snapshot.child("donaterEmail").getValue().toString().equals(email) && snapshot.child("meetingLocation").getValue().toString().equals(location) && snapshot.child("itemDescription").getValue().toString().equals(item) && snapshot.child("status").getValue().toString().equals("Accepted") )
                {
                    itemList1.add(new DonationRequestDetailsModel(snapshot.child("benefactorName").getValue().toString(), snapshot.child("benefactorEmail").getValue().toString(), snapshot.child("dateandtime").getValue().toString(), snapshot.child("remark").getValue().toString()));
                }
                else if (snapshot.child("donaterEmail").getValue().toString().equals(email) && snapshot.child("meetingLocation").getValue().toString().equals(location) && snapshot.child("itemDescription").getValue().toString().equals(item) && snapshot.child("status").getValue().toString().equals("Pending") )
                {
                    itemList2.add(new DonationRequestDetailsModel(snapshot.child("benefactorName").getValue().toString(), snapshot.child("benefactorEmail").getValue().toString(), snapshot.child("dateandtime").getValue().toString(), snapshot.child("remark").getValue().toString()));
                }
            }
            firebaseCallback.onCallBack(itemList1, itemList2);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError)
        {

        }
    });

    xreference.addChildEventListener(new ChildEventListener()
    {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
        {
            if(check)
            {
                itemList2.add(new DonationRequestDetailsModel(dataSnapshot.child("benefactorName").getValue().toString(), dataSnapshot.child("benefactorEmail").getValue().toString(), dataSnapshot.child("dateandtime").getValue().toString(), dataSnapshot.child("remark").getValue().toString()));
                confirmationAdapter1.notifyDataSetChanged();
            }
            else
            {

            }
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
        {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot)
        {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
        {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError)
        {

        }
    });
}

}

但是,当我从页面返回并再次单击页面时,addListenerForSingleValueEvent 的“onDataChange”在 addChildEventListener 的“onChildAdded”之前被触发,为什么?

标签: androidfirebasefirebase-realtime-database

解决方案


推荐阅读