Search code examples
javaandroidandroid-recyclerviewimageviewretrofit

How to show an image from Retrofit response Java android?


I am trying to show images in recyclerView which are Arraylist responses from a database using Retrofit.

my question is that how can I set image to getter and setter class that I have made for my responses?

my getter/setter class:

public class Products implements Parcelable {
    private int id;
    private String title;
    private int price;
    private String image;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public int getPrevious_price() {
        return previous_price;
    }

    public void setPrevious_price(int previous_price) {
        this.previous_price = previous_price;
    }

    private int previous_price;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.title);
        dest.writeInt(this.price);
        dest.writeString(this.image);
        dest.writeInt(this.previous_price);
    }

    public void readFromParcel(Parcel source) {
        this.id = source.readInt();
        this.title = source.readString();
        this.price = source.readInt();
        this.image = source.readString();
        this.previous_price = source.readInt();
    }

    public Products() {
    }

    protected Products(Parcel in) {
        this.id = in.readInt();
        this.title = in.readString();
        this.price = in.readInt();
        this.image = in.readString();
        this.previous_price = in.readInt();
    }

    public static final Parcelable.Creator<Products> CREATOR = new Parcelable.Creator<Products>() {
        @Override
        public Products createFromParcel(Parcel source) {
            return new Products(source);
        }

        @Override
        public Products[] newArray(int size) {
            return new Products[size];
        }
    };
}

my Retrofit interface:

public interface Api {
    

    @GET(url)
    Single<List<Products>> getProducts();
}

my api class:

public class RetApiService {

    public Api api;

    private final String TAG = "RetApiService";
    

    public RetApiService(Context context){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
        api = retrofit.create(Api.class);
    }

    public Single<List<Products>> getProducts(){
        return api.getProducts();
    }

}

and recyclerView adapter class which I have problem here:

public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProductsViewHolder> {

    List<Products> products;

    public ProductsAdapter(List<Products> products){
        this.products= products;
    }

    @NonNull
    @Override
    public ProductsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ProductsViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ProductsViewHolder holder, int position) {
        holder.onBinder(products.get(position));
    }

    @Override
    public int getItemCount() {
        return products.size();
    }

    public class ProductsViewHolder extends RecyclerView.ViewHolder {

        private ImageView productImage;
        private TextView productName;
        private TextView productPrice;
        private TextView productDiscount;
        public ProductsViewHolder(@NonNull View itemView) {
            super(itemView);
            productImage = itemView.findViewById(R.id.productIv);
            productName = itemView.findViewById(R.id.productTitleTv);
            productPrice = itemView.findViewById(R.id.currentPriceTv);
            productDiscount = itemView.findViewById(R.id.previousPriceTv);
        }

        public void onBinder(Products products){
            productName.setText(products.getTitle());
            productPrice.setText(Integer.toString(products.getPrice()));
            productDiscount.setText(Integer.toString(products.getPrevious_price()));
            products.setImage();
        }
    }


}

again my question is that what value should I set in "products.setImage();" in last line of RecyclerView adapter class because I have no idea :D? and sorry I am quite new.


Solution

  • I've found out my answer. I've just used Picasso for that.

    Picasso.get().load(products.getImage()).into(productImage);