-1

I need to fetch the dish name from menu table based on the restaurantID in the restaurant table. Mt database structure: enter image description here

Being new to firebase, I'm not able to find the starting point on how to query through the data.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

Add one field in restaurants id in every dishes. Then create the Class model for Dish.

Now fetch dish data from Firebase.

mFirebaseRef = new Firebase(FIREBASE_URL);
List<Dish_Model> list=new ArrayList();
mFirebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
           Dish_Model dish= postSnapshot.getValue(Dish_Model.class);
           list.add(dish);// here you will get All dish data from firebase
       }
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
 });

At last You need filter data as per restaurants id Suppose you have String restaurants_id="xcnvkncxvkjnxj". Create list for restaurant dishes

  List<Dish_Model> restaurant_dishes=new ArrayList();
  for(Dish_Model  dish:list)
   {
    String[] separated = restaurants_id.split(",");
       for (String id:separated )
         {
           if(id==restaurants_id)
              {
               /*if current dish contain restaurants id then dish will add in 
                  restaurant dishes*/
               restaurant_dishes.add(dish);
                break;
                /*then we break the loop bcoz same dish will not have multiple same 
                 restaurants_id*/
               }
         }
    }

That's done. restaurant_dishes contain all the dishes of restaurant.

Rahul sharma
  • 1,492
  • 12
  • 26