无法从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;
}
}

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,都需要这样做。

相关内容

  • 没有找到相关文章

最新更新