following is my schema where one user has assigned specific orders and one order could have multiple items
Master JSON
-----------
users
user-id
email
username
orders
order-id
orderNo
orderStatus
deliveryDate
items
item-id
itemNo
itemName
itemQty
itemPrice
Related JSON
------------
user-orders
user-id
order-id
orderNo
orderStatus
deliveryDate
order-itmes
order-id
item-id
itemNo
itemName
itemQty
itemPrice
i am accessing all orders belong to one user as follows
dbRef.child("user-orders").child(userId);
and updating listview as follows inside ChildEventListener
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Order order = dataSnapshot.getValue(Order.class);
listview.add(order);
adapter.notifyParentItemInserted(listview.size()-1);
}
but at the same time i want to fetch all items belong to all orders as well , hence i need to use following query inside onChildAdded
and will be needing another ChildEventListener
listener.
dbRef.child("order-items").child(dataSnapshot.getKey());/ /key of newly fetched order.
so question is , firebase has any way to fetch all items belong to all orders inside one listener ? like if i pass all order keys in one go to fetch all items belong to those orders.
i am displaying order and items details on one list only hence i need both data in one go
EDIT
calling orders
public void readOrders(String userId){
mOrderQuery = mDatabaseRef.child("user-orders").child(userId);
mOrderQuery.addChildEventListener(mOrderEventListener);
}
Listener to read orders
private ChildEventListener mOrderEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Order order = dataSnapshot.getValue(Order.class);
orderList.add(order);
// fetching items belong to specific order
mItemQuery = mDatabaseRef.child("order-items").child(dataSnapshot.getKey());
// problem is here for every order i have to register a event listener and this not the right way to do it
mItemQuery.addChildEventListener(mItemEventListener);
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
public void onChildRemoved(DataSnapshot dataSnapshot) { }
public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG , "onCancelled called");
}
};
Listener to read items
private ChildEventListener mItemEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Item item = dataSnapshot.getValue(Item.class);
// here i need to map the incoming item to specific order list
}
public void onChildRemoved(DataSnapshot dataSnapshot) { }
public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
public void onCancelled(DatabaseError databaseError) { }
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.e(TAG , "onChildChanged called");
}
};