您可以在這裏使用DatabaseReference
,因爲它擴展了Query
類。
DatabaseRefernce mRef = FirebaseDatabase.getInstance()
.getReference("menus/menu1/listofproducts");
或者,如果你已經有了一個參考mRef
到要使用root(餐館名稱-f08bd),可以使用下面的查詢
Query menu1ProductsRef = mRef.child("menus/menu1/listofproducts");
然後設置上EventListener
其中之一。
在這兩種查詢,你可以用下面的代碼添加ChildEventListener
DatabaseReference productsRef = mRef.child("products");
menu1ProductsRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
productsRef.child(dataSnapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Add this product to your ListAdapter's ArrayList and notifyDataSetChanged()
//Assuming you have a Product class with the necessary variables, getters and setters for these, and an empty constructor
Product product = dataSnapshot.getValue(Product.class);
productArrayList.add(product);
productListAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//Add the corresponding code for this case
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
//Add the corresponding code for this case
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
//Add the corresponding code for this case
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
})
感謝您的回答,但是這會返回一個圖(字符串,布爾),並且那不是我想要的。我在第一個問題上並不是很清楚,所以我編輯了它,並給了更多信息iam試圖做的事情,希望你現在更清楚我想要什麼,並能幫助我。 – Pawnr
如果你在每個菜單中有不同的產品,那麼最好在菜單節點本身包含所有的產品信息,因爲否則你將不得不對菜單1下的每個產品或查詢全部產品會下載比您需要的更多數據 –
並非所有的時間我都會在每個菜單中使用不同的產品,有時產品可能會在兩個或更多菜單中,並且包括菜單節點本身下的所有信息,都會複製我的數據。我不希望下載比我需要的更多的數據,iam試圖弄清楚,例如,當我在menu1中下載僅屬於該菜單的產品時,然後當我去menu2時只下載屬於menu2等 – Pawnr