获取我的Firebase查询的参考

时间:2017-05-13 21:27:17

标签: android firebase firebase-realtime-database firebase-storage

我正在使用Firebase,我的数据库看起来像

my database screenshot

{
  "CookerInfo" : {
    "org1mLyyJNXtiGo8NimxhWvpId42" : {
      "about" : "Cooker since 2005",
      "address" : "agamy - italy ",
      "available" : "tuesday from 5 am to 9 pm",
      "cookerDishes" : {
        "-Kk3RgXiUEgpgjF1aAdc" : {
          "foodName" : "mol5ia",
          "foodUrl" : "https://firebasestorage.googleapis.com/v0/b/fir-auth-    aa979.appspot.com/o/dishphoto%2F1494723249227.jpg?alt=media&token=0fc03b18-d310-    493a-b332-c1f3e4896567",
          "price" : "8 $",
          "timeToPrepared" : "10 min",
          "weight" : "100 gm "
        }
      },

      "name" : "chef.mohamed",
      "phone" : "0192822228",
      "url" : "https://firebasestorage.googleapis.com/v0/b/fir-auth-aa979.appspot.com/o/cookerPhoto%2F1494723157098.jpg?alt=media&token=19e4e376-0013-4410-bc04-0483aff09902"
    },
    "vuUscH2L7wgtj2N3rGWOOjgv23s2" : {
      "about" : "cookgood",
      "address" : "alexandria",
      "available" : "sunday",
      "cookerDishes" : {
        "-Kk32fy0FO1vtZgm6AOK" : {
          "foodName" : "chiken",
          "foodUrl" : "https://firebasestorage.googleapis.com/v0/b/fir-auth-    aa979.appspot.com/o/dishphoto%2F1494716692079.jpg?alt=media&token=a70ed96e-acbc-45c6-95b7-4811fd8e2e26",
          "price" : "10$",
          "timeToPrepared" : "10 min",
          "weight" : "100gm"
        },
        "-Kk32mfQWiMRLFV2SMo7" : {
          "foodName" : "rise",
          "foodUrl" : "https://firebasestorage.googleapis.com/v0/b/fir-auth-    aa979.appspot.com/o/dishphoto%2F1494716718899.jpg?alt=media&token=6b9007f5-c22e-4ed4-83aa-a80f45e5363f",
          "price" : "8 $",
          "timeToPrepared" : "8 min",
          "weight" : "50 gm"
        }
      },

      "name" : "sherbini",
      "phone" : "01093812681",
      "url" : "https://firebasestorage.googleapis.com/v0/b/fir-auth-aa979.appspot.com/o/cookerPhoto%2F1494716634787.jpg?alt=media&token=2ba72b6d-0af0-4235-bff5-4b706e91486f"
    }
  }
    }

而且我想只为特定的炊具(1个炊具)烹饪食物,将他的菜肴放在gridview中,但是我没有将ID保存在模型中。

我尝试了类似的东西,但它没有用。

Query queryRef=mDatabaseRef.orderByChild("name")
           .equalTo(COOKERNAME);


   DatabaseReference node =queryRef.getRef();


   node.child("cookerDishes").addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {

         Iterable<DataSnapshot> children=dataSnapshot.getChildren();

         for (DataSnapshot child : children){




             FoodDish fooddish = new FoodDish();


             String food_name =child.child("foodName").getValue(String.class);
             fooddish.setFoodName(food_name);



             foodList.add(fooddish);

         }

         gv.setAdapter(adapter);
     }

     @Override
     public void onCancelled(DatabaseError databaseError) {

     }
 });

2 个答案:

答案 0 :(得分:1)

我想你忘了在你的addapter上打电话给notifyDataSetChanged()

答案 1 :(得分:0)

您无需在查询中调用getRef()。相反,您应该将侦听器直接附加到查询:

Query queryRef=mDatabaseRef
       .orderByChild("name")
       .equalTo(COOKERNAME);

queryRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot cookerSnapshot : dataSnapshot.getChildren()){
      DataSnapshot cookerDishes = cookerSnapshot.getChild("cookerDishes"));
      for (DataSnapshot dishSnapshot: cookerDishes.getChildren()) {
         FoodDish fooddish = new FoodDish();
         String food_name =child.child("foodName").getValue(String.class);
         fooddish.setFoodName(food_name);
         foodList.add(fooddish);
     }