在firebase中按范围查询 [英] Querying by range in firebase

查看:58
本文介绍了在firebase中按范围查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于食物价格的范围滑块,根据该滑块的最小值和最大值,我想显示该范围内的食物.

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.

滑子代码

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);
    }
});

查询代码

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());

结构

我尝试使用startAt和endAt,但是它显示的食物价格超出范围.对此有任何帮助吗?还有另一种方式进行这种查询吗?

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?

推荐答案

您正在将数字值作为字符串存储在数据库中. Firebase按字典顺序对字符串进行排序.按照从大到小的顺序,数字从1到12,它们被排序为"1", "10", "11", "12", "2", "3", "4", "5", "6", "7", "8", "9".因此,从"1""10"的范围正是这两个数字".

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.

另请参阅:

  • firebase orderByChild returns weird order for only one child
  • Firebase query by date string
  • firebase database order by value working wrong
  • Firebase query ordering not working properly
  • other questions mentioning lexicographical sorting on Firebase

这篇关于在firebase中按范围查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆