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

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

"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检索值?

gopyfrb3

gopyfrb31#

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

相关问题