android - 使用 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”之前被触发,为什么?
解决方案
推荐阅读
- html - 内容从主容器溢出
- python - 如何通过返回汇总的值来更新字典中的值?
- html - 当我在笔记本电脑(小屏幕)上打开网页时,网页会变形,但在台式电脑(大屏幕)上打开它时仍然可以)
- apache-spark - 使用python将databricks数据帧写入S3
- java - 更正应用程序的类路径,使其包含一个兼容的 javax.persistence.Table 版本
- dynamics-nav - 我们如何在 NAV 2016 中使用 C/AL 执行 CRUD 和 Join 操作?
- python - 我不能在生成器中使用其他人的模型来生成我的输入。我该如何解决?
- python - 比较来自多个数据帧的数据帧以过滤数据并提取相关特征
- sql-server - 更改 sql server 表问题
- c++ - 我不明白如何使用嵌套循环计算每个可能子数组的总和