Android 根据一组类别检索数据
我试图检索一组餐厅菜单数据,根据菜单类别对它们进行分组,为每个菜单类别创建菜单项列表,并使用category键创建这些列表的HashMap 下面的JSON树显示了我如何在餐厅ID下存储每个餐厅的菜单项Android 根据一组类别检索数据,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我试图检索一组餐厅菜单数据,根据菜单类别对它们进行分组,为每个菜单类别创建菜单项列表,并使用category键创建这些列表的HashMap 下面的JSON树显示了我如何在餐厅ID下存储每个餐厅的菜单项 "Menu" : { "C39Jv1oVVdXbQdyCOcTQZyZnmC3x" : { //Restaurant ID "applepie" : { "item_category" : "Desserts", "item_name"
"Menu" : {
"C39Jv1oVVdXbQdyCOcTQZyZnmC3x" : { //Restaurant ID
"applepie" : {
"item_category" : "Desserts",
"item_name" : "Apple Pie",
"item_price" : "280"
},
"chocopudding" : {
"item_category" : "Desserts",
"item_name" : "Chocolate Pudding",
"item_price" : "320"
},
"friedrice" : {
"item_category" : "Main Courses",
"item_name" : "Fried Rice",
"item_price" : "250"
},
"seafoodrice" : {
"item_category" : "Main Courses",
"item_name" : "Sea Food Fried Rice",
"item_price" : "300"
}
}
}
每个餐厅的食品类别存储在MenuCategories
节点中
"MenuCategories" : {
"C39Jv1oVVdXbQdyCOcTQZyZnmC3x" : { //Restaurant ID
"Desserts " : true,
"Main Courses" : true
}
}
根据上述数据集,我试图实现如下目标:
mDatabase = FirebaseDatabase.getInstance().getReference().child("Menu").child(res_id);
mMenuCategoryRef = FirebaseDatabase.getInstance().getReference().child("MenuCategories").child(res_id);
数据检索:
List<String> menuCatList = new ArrayList<>();
mMenuCategoryRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data : dataSnapshot.getChildren()){
menuCatList.add(data.getKey()); //adds all the categories to array list
}
for(String menuCat : menuCatList){
//I'm trying to retrieve items under each category
mDatabase.orderByChild("item_category").equalTo(menuCat).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data : dataSnapshot.getChildren()){
System.out.println("MenuListWithCat "+data.getKey()+" - "+data.getValue());
}
}
.....
它完全跳过了“甜点”类别,这是为什么
任何与此相关的帮助都会非常有帮助。我能够找到答案。为了为每个类别创建单独的菜单项列表,我使Firebase数据检索过程复杂化,我决定检索所有菜单项并将其添加到ArrayList,而不考虑其类别类型,如下所示:
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
Menu menu = child.getValue(Menu.class);
menuList.add(menu);
}
然后根据菜单项的类别对所有菜单项进行分组,并使用Java逻辑将它们添加到单独的列表中,这为我提供了以下格式的HashMap:
Map<ItemCategory, ListofItems>
Map
逻辑是这样的:
Map<String, List<Menu>> menuMap= new HashMap<String, List<Menu>>();
for(Menu menuItem : menuList){
List<Menu> tempList = menuMap.get(menuItem.getItem_category());
if(tempList == null){
tempList = new ArrayList<Menu>();
menuMap.put(menuItem.getItem_category(), tempList);
}
tempList.add(menuItem);
}
Map menuMap=newhashmap();
用于(菜单菜单项:菜单列表){
List templast=menuMap.get(menuItem.getItem_category());
if(templast==null){
templast=newarraylist();
menuMap.put(menuItem.getItem_category(),templast);
}
添加(菜单项);
}
正如Frank在评论中提到的,我从数据库中删除了不需要的MenuCategories
节点
我希望这会对将来的人有所帮助!:)
特别感谢@Frank提供宝贵的反馈和支持。如果您在
onDataChange
中为mMenuCategoryRef
添加日志语句,它会打印吗?换句话说:是否无法检索类别本身?还是在内部循环中检索这些类别的菜单项失败。@FrankvanPuffelen是的,两种类别类型都被检索并添加到menuCatList
。也许我用来在菜单节点下存储菜单项的结构不合适,有什么想法吗?嗯。。。。您的数据结构看起来确实有些不同寻常。既然您已经在/Menu/$restaurantId
下列出了每个餐厅的项目类别,我真的不明白您为什么需要/MenuCategories/$restaurantId
。但是代码看起来不错,所以我不确定为什么它没有读取内部循环中的菜单项。也许是别人发现了问题所在。@FrankvanPuffelen是的,我同意,我能够解决它。请核对我的答案。非常感谢你。
Map<String, List<Menu>> menuMap= new HashMap<String, List<Menu>>();
for(Menu menuItem : menuList){
List<Menu> tempList = menuMap.get(menuItem.getItem_category());
if(tempList == null){
tempList = new ArrayList<Menu>();
menuMap.put(menuItem.getItem_category(), tempList);
}
tempList.add(menuItem);
}