-1

I'm attempting to display an image from my SQLite database on a recycler view. The images are links I scraped from a second-hand car website. So far I was able to display the name, price, year and location. I looked at Picasso on how to do it but I can't seem to find examples on this platform on how to integrate it on a recyclerview. Does anyone have an idea of how to display it?

class SearchViewHolder extends RecyclerView.ViewHolder {
    public TextView name, price, year, location;
    public ImageView image;



    public SearchViewHolder(@NonNull View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name);
        price = (TextView) itemView.findViewById(R.id.price);
        year = (TextView) itemView.findViewById(R.id.year);
        location = (TextView) itemView.findViewById(R.id.location);
        image = (ImageView) itemView.findViewById(R.id.car_img);
    }
}

public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder> {

    private Context context;
    private List<Products> products;

    public SearchAdapter(Context context, List<Products> products) {
        this.context = context;
        this.products = products;
    }

    @NonNull
    @Override
    public SearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.layout_item,parent, false);
        return new SearchViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchViewHolder holder, int position) {
        holder.name.setText(products.get(position).getName());
        holder.price.setText(products.get(position).getPrice());
        holder.year.setText(products.get(position).getYear());
        holder.location.setText(products.get(position).getLocation());

    }

    @Override
    public int getItemCount() {
        return products.size();
    }
}
bwaife
  • 23
  • 6
  • If your trouble is setting the image in an `ImageView`, which is what I presume `holder.image` references, then see https://stackoverflow.com/a/2974902/13373270. – codebod Feb 18 '21 at 19:30
  • It didn't really help but thanks for the help – bwaife Feb 18 '21 at 19:46

1 Answers1

2

You can use Glide library

def glide_version = "4.3.1"

implementation "com.github.bumptech.glide:glide:$glide_version"
annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"

then implement it

Glide.with(context)
       .load(imageUrl)
       .into(imageview);

Or You can use Picasso library

def picasso_version = "2.5.2"

implementation "com.squareup.picasso:picasso:$picasso_version"

Then implement it

Picasso.with(getContext()).load(imageUrl).into(imageView);

For your Case: Put it in your adapter class like that:

    @Override
    public void onBindViewHolder(@NonNull SearchViewHolder holder, int position) {
        holder.name.setText(products.get(position).getName());
        holder.price.setText(products.get(position).getPrice());
        holder.year.setText(products.get(position).getYear());
        holder.location.setText(products.get(position).getLocation());

        Glide.with(context)
          .load(products.get(position).getImage())
          .into(holder.image);
    }
Marwa Eltayeb
  • 1,921
  • 1
  • 17
  • 29