如何从 firebase 中检索值(一对多关系)?
How can I retreive values from firebase (one to many relationship)?
"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 returns true
如何从具有一对多关系的 firebase 中检索值?
您正在尝试从 "item01": true
节点读取 name
、description
和其他属性。这行不通,因为该节点上不存在属性。
您需要加载该特定项目的快照,然后从 that 中读取属性。这是相当重要的,所以我强烈建议使用专门为此案例制作的 setIndexedQuery
method。
"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 returns true 如何从具有一对多关系的 firebase 中检索值?
您正在尝试从 "item01": true
节点读取 name
、description
和其他属性。这行不通,因为该节点上不存在属性。
您需要加载该特定项目的快照,然后从 that 中读取属性。这是相当重要的,所以我强烈建议使用专门为此案例制作的 setIndexedQuery
method。