Skip to content
Advertisement

Adapter doesn’t show product image and price from Firebase Realtime Database

I am trying to retrieve data (a product image and the price for it) from Firebase Realtime Database and put it in a recyclerView, but I have some difficulties. In my database I stored the image url for one product and I want to show that image in an ImageView component placed in the cardView. I also managed to retrieve the url and price correctly, but my recyclerView doesn’t show them. This is my product class:

JavaScript

My Adapter as following:

JavaScript

And the activity I have placed my recyclerView:

JavaScript

The product name is correctly because is a variable, but the image and price don’t change at all. The “for” loop serves in creating as much cardViews as my products of that type are in checkout. How can I make it show image and price from database?

This is my Firebase child structure:

Firebase Database

EDIT Updated code:

JavaScript

And in onBindViewHolder:

JavaScript

Advertisement

Answer

customized.add(new CustomizeProduct(productImage,checkout.thisProductName, productPrice));

You can see only product name (checkout.thisProductName), but not product image & price because you populate your adapter with product names that is not returned by Firebase in your onDataChange(DataSnapshot ds) callback; there is no clue in your code how do you build product names (checkout.thisProductName) value.

Now product images & prices not shown as you build the adapter in the main thread, while firebase works implicitly in background thread; so you need to sync your adapter to that background thread by transferring the part of code that builds your recyclerView adapter into the firebase callback as follow:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement