Ayushi Indani
Ayushi Indani

Reputation: 61

How to filter firebase data results by applying multiple Query in Android?

My database Structure is as follows

I want to display all the worker names whose city="Varanasi" and gender="Male" and salary is between 5000 to 15000 in a Recycler View. In this case the name of the shown worker must be displayed. What query must be applied and how should it be achieved?

Upvotes: 2

Views: 5361

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138804

Acording to this tutorial, The Firebase Database For SQL Developers you can create a new field for each node named: city_gender. For you particular example the value of this field will be Varanasi_Male. This how it should look like:

city_gender: "Varanasi_Male"

In this way, you can query your Firebase database for every worker who is a male and works in Varanasi. In the same way you can achieve this for intervals.

Hope it helps.

Upvotes: 1

danflzz
danflzz

Reputation: 44

First, I think you need save the salary with double value, not string.

You need your reference at worker node, then add a listener and iterate the values. Also you need a worker class to obtain the data.

yourReference.orderByChild("city").equalTo("Varanasi").addListenerForSingleValueEvent(){

    @Override
    public void onDataChange(Datasnapshot data){
       for(Datasnapshot d: data.getChildren()){
         yourClass work = d.getValue(yourClass.class);
         if(work.getGender().equals("Male"){
            double salary = work.getSalary();
           if(salary  > 5000 && salary < 15000){
                 //save the worker into a list. 
            }
         } 
       }
      }
    }
}

Upvotes: 2

Related Questions