Andrew
Andrew

Reputation: 57

Filter Realtime Database Android

I want to create an application that any user can add an advert (announce) with something that he wants to sell. This is the structure of the Real-time Database in Firebase:

- advert
    - category
        - {uid}
            - title
            - price
            - description
            - location

I want to let the user to filter the adverts by category, title, and price.

Currently, I can filter the adverts only by title like this:

FirebaseDatabase.getInstance().getReference()
                    .child("adverts")
                    .orderByChild("title")
                    .startAt("tv")
                    .limitToFirst(20)

I have 3 questions:

  1. How can i add more "and clauses"? Or how should i structure the nodes to filter more values?
  2. How should I sort the values that i receive from the server in ascending order or descending order depending on uid?
  3. Seems that the method startAt("abc") is not fetching only the children that has contains in the title "tv". Am i doing something wrong? How should I fetch only the children that contains the string "tv"?

Upvotes: 2

Views: 1195

Answers (1)

Rosário P. Fernandes
Rosário P. Fernandes

Reputation: 11344

  1. On firebase, there is no way to add "AND clauses". You must structure your data in a way that matches your filtering needs.

  2. I believe firebase already sorts the data by uid.

  3. If you want to fetch only the children that contains the string "tv", you should use this query:

    FirebaseDatabase.getInstance().getReference().child("adverts").orderByChild("title").startAt("tv").endAt("tv").limitToFirst(20);

I suggest that you watch the Firebase Database for SQL Developers Series. It has the answers for the 3 of your questions

Upvotes: 1

Related Questions