2

I want to use multiple filters like SELECT * table_name WHERE age=20 && country = UK && weight=60 in firebase database.

My code:

 private void loadListFood(String catagoryId) {
    adapter=new FirebaseRecyclerAdapter<Food, FoodViewHolder>(Food.class,
            R.layout.food_item,
            FoodViewHolder.class,

            foodList.orderByChild("menuId").equalTo(catagoryId)

    ) {
        @Override
        protected void populateViewHolder(FoodViewHolder foodViewHolder, Food food, int i) {
            foodViewHolder.food_name.setText(food.getName());
            Picasso.with(getBaseContext()).load(food.getImage())
                    .into(foodViewHolder.food_image);

            final Food local=food;
            foodViewHolder.setItemClickListener(new ItemClickListner() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                    //Toast.makeText(FoodList.this, ""+local.getName(), Toast.LENGTH_SHORT).show();
                    Intent foodDetail = new Intent(FoodList.this,FoodDetail.class);
                    String FoodID=adapter.getRef(position).getKey().toString();
                    foodDetail.putExtra("FoodId",FoodID);
                    startActivity(foodDetail);


                }
            });

        }
    };

    recyclerView.setAdapter(adapter);
}

So I want to load menuID = categoryID and status==available items into recyclerview from firebase database

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ishara96
  • 25
  • 5
  • Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Apr 14 '20 at 13:30

2 Answers2

1

In realtime database, you cannot use AND query. Instead you can combine those attributes into one attribute:

Users
  randomId
       age :20
       country: UK
       weight : 60
       age_country_weight : 20_UK_60

Then you can do:

foodList.orderByChild("age_country_weight").equalTo("20_UK_60")
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
1

Firebase Realtime Database does not support multiple equalTo. If you want continue use Realtime Database you will need to structure your data differently in so you can query based on one equalTo in combination with orderByChild.

But you can avoid this limitation by switching on Firestore which supports Query based on multiple whereEqualTo for example:

Query query = collectionRefference.
.whereEqualTo("menuID", "categoryID")
.whereEqualTo("status", "available");
Yupi
  • 4,402
  • 3
  • 18
  • 37