0

I have a range slider for prices of food , and based on the min and max of the slider , I want to display the foods which are in this range.

Slider code

multiSlider.setOnThumbValueChangeListener(new MultiSlider.SimpleChangeListener() {
    @Override
    public void onValueChanged(MultiSlider multiSlider, MultiSlider.Thumb thumb, int thumbIndex, int value) {
        if (thumbIndex == 0) {
            min = String.valueOf(value);
            min1.setText(min);
        } else {
            max = String.valueOf(value);
            max1.setText(max);
        }
    }
});

alertD.setPositiveButton("Done", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        SearchPrice(min, max);
    }
});

Query Code

private void SearchPrice(String min, String max) {
    Query searchByName = foodList.orderByChild("price").startAt(min).endAt(max);
    FirebaseRecyclerOptions<Food> foodOptions = new FirebaseRecyclerOptions.Builder<Food>().setQuery(searchByName, Food.class).build();

    Searchadapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(foodOptions) {
        @Override
        protected void onBindViewHolder(@NonNull FoodViewHolder viewHolder, int position, @NonNull Food model) {
            viewHolder.food_name.setText(model.getName());
            viewHolder.food_price.setText(model.getPrice());

Structure

enter image description here

I tried using the startAt and endAt , but its displaying food with prices outside of the range.Any help on this please ? Is there another way of doing this type of query?

Stefano Tokyo
  • 85
  • 1
  • 10
  • 2
    Adding the firebase database structure might help us to be able to help you maybe? – ʍѳђઽ૯ท Oct 09 '18 at 20:28
  • 2
    You should also simplify the query code you posted. Try to pick out only the code concerned with firebase readtime database query – rzymek Oct 09 '18 at 20:31
  • Please have a look , i added the structure. – Stefano Tokyo Oct 09 '18 at 20:34
  • @StefanoTokyo What it returns now? What's in the min - max value? would you add the whole query and the values of min -max too? However, I think you missed: `orderByKey()` before startAt and ... – ʍѳђઽ૯ท Oct 09 '18 at 20:37
  • 1
    My first guess is that you have `min` and `max` as numbers, while you the value of `price` in your database is a string. If that's the cause, try `foodList.orderByChild("price").startAt(""+min).endAt(""+max)` – Frank van Puffelen Oct 09 '18 at 20:37
  • How about this? `orderByKey().startAt("1").endAt("0");` – ʍѳђઽ૯ท Oct 09 '18 at 20:41
  • orderByKey() doesnt return anything. Lets say my min is "1" and my max is "10".It will return a food with the price of "10".But I have foods with price of "2" and "3".Why they aren't displaying too ? – Stefano Tokyo Oct 09 '18 at 20:50
  • Because you're storing numbers as strings, and strings are sorted lexicographically. In lexicographical order of you have number from 1 to 12, they are sorted as `"1", "10", "11", "12", "2", "3", "4", "5", "6", "7", "8", "9"`. So the range from `"1"` to `"10"` is precisely those two "numbers". – Frank van Puffelen Oct 10 '18 at 03:43

1 Answers1

2

You're storing numeric values as strings in the database. Firebase sorts strings lexicographically. In lexicographical order of you have number from 1 to 12, they are sorted as "1", "10", "11", "12", "2", "3", "4", "5", "6", "7", "8", "9". So the range from "1" to "10" is precisely those two "numbers".

To solve this problem, store your numeric values as actual numbers in the database.

Also see:

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