Skip to content

How can I retreive values from firebase (one to many relationship)?

[![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);
}

I can’t access name, imageurl and other properties because snapshot.getvalue returns true How can I retreive values from firebase having one to many relationship?

Answer

You’re trying to read the name, description and other properties from the "item01": true node. That won’t work, since the properties don’t exist on that node.

You’ll instead need to load the snapshot for that specific item, and then read the properties from that. This is fairly non-trivial so I highly recommend using the setIndexedQuery method that was specifically made for this case.