我正在尝试在同一页面上显示来自两个不同数据库的数据。我使用FirebaseRecyclerAdapter
。我的问题是,当我打开该页面时,仅显示第一个数据,当我关闭它并再次打开时,则显示两个数据页面。当我使用相同的数据库时,第一次访问时都会显示两个数据。知道这有什么解决方案吗?我试图查看查询是否错误,但是第一次甚至第二次都没有打印任何东西。
ordersDatabase.keepSynced(true);
ordersDoneDatabase.keepSynced(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(OldOrdersActivity.this);
LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(OldOrdersActivity.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager2.setReverseLayout(true);
linearLayoutManager2.setStackFromEnd(true);
activeOrders.setHasFixedSize(true);
doneOrders.setHasFixedSize(true);
activeOrders.setLayoutManager(linearLayoutManager);
doneOrders.setLayoutManager(linearLayoutManager2);
final Query orderByUserId = ordersDatabase.orderByChild("curUser").equalTo(current_user_id);
FirebaseRecyclerAdapter<Order, OldOrdersActivity.OrdersViewHolder> firebaseOrderAdapter = new FirebaseRecyclerAdapter<Order, OldOrdersActivity.OrdersViewHolder>(
Order.class,
R.layout.model,
OldOrdersActivity.OrdersViewHolder.class,
orderByUserId
) {
@Override
protected void populateViewHolder(final OldOrdersActivity.OrdersViewHolder ordersViewHolder, final Order order, int i) {
final String orderKey = getRef(i).getKey();
DatabaseReference myOrders = rootRef.child("Orders").child(orderKey);
myOrders.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {...
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
};
和第二个Firebase适配器:
final Query orderByStatusTrue = ordersDoneDatabase.child(firebaseAuth.getUid()).orderByChild("orderStatus").equalTo(true);
FirebaseRecyclerAdapter<Order, OldOrdersActivity.OrdersViewHolder> firebaseOrderAdapterStatusTrue = new FirebaseRecyclerAdapter<Order, OldOrdersActivity.OrdersViewHolder>(
Order.class,
R.layout.model,
OldOrdersActivity.OrdersViewHolder.class,
orderByStatusTrue
) {
@Override
protected void populateViewHolder(final OldOrdersActivity.OrdersViewHolder ordersViewHolder, final Order order, int i) {
final String orderKey = getRef(i).getKey();
DatabaseReference myOrders = rootRef.child("Orders_done").child(current_user_id).child(orderKey);
myOrders.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
...
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
};
activeOrders.setAdapter(firebaseOrderAdapter);
doneOrders.setAdapter(firebaseOrderAdapterStatusTrue);