如何从firebase检索值(一对多关系)?

cfh9epnr  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(532)

  1. "item03" : {
  2. "description" : "...... ",
  3. "image_url" : "....",
  4. "name" : ".....",
  5. "price" : "250.00"
  6. }
  1. item = FirebaseDatabase.getInstance().getReference("Category")
  2. .child(categoryID).child("Items");
  1. private void fetchData() {
  2. FirebaseRecyclerOptions<ItemModel> options = new FirebaseRecyclerOptions.Builder<ItemModel>()
  3. .setQuery(item, new SnapshotParser<ItemModel>() {
  4. @NonNull
  5. @Override
  6. public ItemModel parseSnapshot(@NonNull DataSnapshot snapshot) {
  7. return new ItemModel(snapshot.child("name").getValue().toString(),
  8. snapshot.child("description").getValue().toString(),
  9. snapshot.child("image_url").getValue().toString(),
  10. snapshot.child("price").getValue().toString());
  11. }
  12. }).build();
  13. // Set the adapter
  14. itemsAdapter = new ItemsRecyclerViewAdapter(options);
  15. }

我无法访问name、imageurl和其他属性,因为snapshot.getvalue返回true如何从具有一对多关系的firebase检索值?

gopyfrb3

gopyfrb31#

你在试着读 name , description 以及其他来自 "item01": true 节点。这行不通,因为该节点上不存在属性。
相反,您需要加载该特定项的快照,然后从中读取属性。这是非常重要的,所以我强烈建议使用 setIndexedQuery 专门为这个病例设计的方法。

相关问题