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

时间:2020-12-20 16:06:18

标签: java android firebase snapshot

[![enter image here][

"item03" : {
    "description" : "...... ",
    "image_url" : "....",
    "name" : ".....",
    "price" : "250.00"
}
item = FirebaseDatabase.getInstance().getReference("Category")
        .child(categoryID).child("Items");
private void fetchData() {
    FirebaseRecyclerOptions<ItemModel> options = new FirebaseRecyclerOptions.Builder<ItemModel>()
            .setQuery(item, new SnapshotParser<ItemModel>() {
        
        @NonNull
        @Override
        public ItemModel parseSnapshot(@NonNull DataSnapshot snapshot) {
            return new ItemModel(snapshot.child("name").getValue().toString(),
                    snapshot.child("description").getValue().toString(),
                    snapshot.child("image_url").getValue().toString(),
                    snapshot.child("price").getValue().toString());
        }
    }).build();

    // Set the adapter
    itemsAdapter = new ItemsRecyclerViewAdapter(options);
}

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

1 个答案:

答案 0 :(得分:1)

您正在尝试从 name 节点读取 description"item01": true 和其他属性。这是行不通的,因为该节点上不存在这些属性。

您需要加载该特定项目的快照,然后从那个读取属性。这相当重要,因此我强烈建议使用专为此案例制作的 setIndexedQuery method

相关问题