首页 > 解决方案 > 无法从 firebase 回收器视图中的 firebase 实时数据库中获取嵌套数据

问题描述

我正在尝试从 firebase 数据库中获取数据并在回收器视图中显示它,当没有嵌套数据但不适用于嵌套数据时,它工作得非常好,请帮助我,我在这里附上所有的屏幕截图。

JSON 上传到 firebase:

{
  "orders": {
    "order1": {
        "Customer": "Ankit",
        "Description": "Make it spicy",
        "Food": {
            "Paneer": {
                "Full_Plate": 1,
                "Half_Plate": 1
            },
            "Roti": {
                "Full_Plate": 1,
                "Half_Plate": 2
            }
        },
        "Header": "table1"
    },
    "order2": {
        "Customer": "Raghav",
        "Description": "Creamy",
        "Food": {
            "Chicken": {
                "Full_Plate": 1,
                "Half_Plate": 1
            }
        },
        "Header": "Table2"
    }
  }
}

我的onCreateView()代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view= inflater.inflate(R.layout.fragment_recfragment, container, false);
            recview=(RecyclerView)view.findViewById(R.id.recview);
            recview.setLayoutManager(new LinearLayoutManager(getContext()));
            FirebaseRecyclerOptions<Model> options=
                    new FirebaseRecyclerOptions.Builder<Model>()
                            .setQuery(FirebaseDatabase.getInstance().getReference().child("orders"),Model.class)
                            .build();
            myAdapter=new MyAdapter(options);
            recview.setAdapter(myAdapter);
            return view;

        }

模型类代码:

public class Model {
    String Customer,Description,Header;
    HashMap<String,ArrayList<Integer>> Food;

    public Model(){

    }

    public Model(String customer, String description, String header, HashMap<String, ArrayList<Integer>> food) {
        Customer = customer;
        Description = description;
        Header = header;
        Food = food;
    }

    public String getCustomer() {
        return Customer;
    }

    public void setCustomer(String customer) {
        Customer = customer;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getHeader() {
        return Header;
    }

    public void setHeader(String header) {
        Header = header;
    }

    public HashMap<String, ArrayList<Integer>> getFood() {
        return Food;
    }

    public void setFood(HashMap<String, ArrayList<Integer>> food) {
        Food = food;
    }
}

标签: androidfirebasefirebase-realtime-databasefirebaseui

解决方案


Firebase 使用 JavaBean 约定来确定 JSON 中的属性名称。这意味着类似的方法public String getCustomer()会转换为customer数据库中的属性。注意到那里套管的不同了吗?这很可能是您的问题的原因。

如果要维护数据库中的大小写,可以在 Java 类上添加注释以显式指定属性名称:

@PropertyName("Customer")
public String getCustomer() {
    return Customer;
}

@PropertyName("Customer")
public void setCustomer(String customer) {
    Customer = customer;
}

您需要对数据库中的名称/大小写与 Java 类中的名称/大小写不完全匹配的每个 getter/setter 执行此操作。


推荐阅读