简体   繁体   中英

Android Firestore compound query

I am developing a car rental android application in Android Studio. I am working on a "Filter" option, so the users can filter available cars (for example they can choose to see only diesel cars with 5 seats or they can choose to see only all-wheel-drive cars, etc.). I have an activity called FilterCars where the user for example can filter by 4 components: fuel, traction, seats, gearbox. After they submit the filter, the cars are shown based on their preference. I pass those filters to Cars activity(where the cars are shown in a RecyclerView) with bundle extras with success, so I have in Cars.java stored with success user's filter in variables "fuelType, tractionType, seatsType, gearboxType".

With those filters I can pass to the recyclerview adapter a query like:

FirebaseFirestore.getInstance().collection("Cars").whereEqualTo("fuel", fuelType).whereEqualTo("traction", tractionType).whereEqualTo("seats", seatsType).whereEqualTo("gearbox", gearboxType)

which is working just fine.

My problem is the following: I don't want to force the users to complete all the filter fields, so for example, if they want to filter only by fuel and seats, they can do it. How can I make a compound query which only contains the filters chosen by the user? One way I thought about it but I can't resolve it is if, for example, the user doesn't want to filter by traction, to store in tractionType something like " " (empty), and in the compound query instead of tractionType query "whereEqualTo" to put something which returns all the cars, so the compound query will filter only by the chosen filters.

Any suggestion on how to do it? Or is there a better way in which I can avoid putting the empty answers in the compound query?

Thank you!

The following query:

FirebaseFirestore.getInstance().collection("Cars")
        .whereEqualTo("fuel", fuelType)
        .whereEqualTo("traction", tractionType)
        .whereEqualTo("seats", seatsType)
        .whereEqualTo("gearbox", gearboxType);

Will filter your collection "Cars" by the value of four properties.

I don't want to force the users to complete all the filter fields, so for example if they want to filter only by fuel and seats

There are two ways in which you can solve this issue. Taking as an example, the above query, you can remove the calls to ".whereEqualTo("traction", tractionType)" and ".whereEqualTo("gearbox", gearboxType)" and you'll have the desired result. Or, you can do as following, as it makes more sense in my opinion.

When the user opens the app for the first time, simply display all available cars that exist within your "Cars" collection:

Query query = FirebaseFirestore.getInstance().collection("Cars")

If there are too many, add a limit() call, or implement pagination. Then simply add the possibility to select the car filters. For example, add 4 check-boxes for each filtering option. Once an option is selected, add it to your Query object like so:

query = query.whereEqualTo("fuel", fuelType)

If the user selects all options, then the query will look like the first one.

Please also remember, that Firestore queries are immutable. For more info, please check my answer from the following post:

If are using for example Firebase-UI library for Android, don't forget to start/stop listening after you set a new Query. If you aren't using the library, don't forget to notify the adapter about the changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM