您可以在这里使用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