如何将图像添加到AdapterClass中的RecyclerView?

问题描述 投票:-1回答:7

这是Product bean类enter image description here

所以我的Recycle视图正在运行,但是这行代码没有用。这来自My Adapter Class。

holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));

@NonNull
@Override
public productViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.list_layout,null);
    productViewHolder holder = new productViewHolder(view);
    return holder;
    //  return new productViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull productViewHolder holder, int position) {

    Product product = productList.get(position);
    holder.textViewTitle.setText(product.getTitle());
    holder.textViewDescription.setText(product.getShort_description());
    holder.textViewRating.setText(String.valueOf(product.getRating()));
    holder.textViewPrice.setText(String.valueOf(product.getPrice()));

//    holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));

}

@Override
public int getItemCount() {
    //returns number of elements within the list
    return productList.size();
}

class productViewHolder extends RecyclerView.ViewHolder{

    ImageView imageView;
    TextView textViewTitle, textViewDescription, textViewRating, textViewPrice;


    public productViewHolder(@NonNull View itemView) {
        super(itemView);

        imageView = itemView.findViewById(R.id.imageView);
        textViewTitle = itemView.findViewById(R.id.textViewTitle);
        textViewDescription = itemView.findViewById(R.id.textViewShortDesc);
        textViewRating = itemView.findViewById(R.id.textViewRating);
        textViewPrice = itemView.findViewById(R.id.textViewPrice);
    }
android android-studio android-recyclerview recycler-adapter
7个回答
0
投票

如果产品对象中的图像包含可绘制包中的图像资源,则需要使用

holder.imageView.setImageResource(product.getImage());

0
投票

试试这个:

如果您获取数据表单api设置图像这样

 RequestOptions options = new RequestOptions()
            .centerCrop()
            .placeholder(R.drawable.avatar1)
            .error(R.drawable.avatar1)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .priority(Priority.HIGH)
            .dontAnimate()
            .dontTransform();

 Glide.with(context)
            .load(UpcomingHistoryList.get(position).getPhotoUrl())
            .apply(options)
            .into(holder.img);

你可以使用滑翔和毕加索

如果你得到图像形式drawable设置像这样的图像

  ImageView imageView = (ImageView) findViewById(R.id.image);
  imageView.setImageResource(R.drawable.hello);

0
投票

我认为你得到了一个产品对象的String,你应该尝试将该estring转换为Uri或bitmap,这样当你在imageview中创建一个set然后你可以展示时,如果你展示你的产品类知道它会有很大帮助什么类型的值返回product.getimage(); Uri.parse(product.getImage());


0
投票

有一种方法可用于加载可绘制文件,它们是vector isset

 public static BitmapDescriptor getBitmapFromVector(@NonNull Context context,
                                                   @DrawableRes int vectorResourceId,
                                                   @ColorInt int tintColor) {

    Drawable vectorDrawable = ResourcesCompat.getDrawable(
            context.getResources(), vectorResourceId, null);
    if (vectorDrawable == null) {
        String TAG = "tag";
        Log.e(TAG, "Requested vector resource was not found");
        return BitmapDescriptorFactory.defaultMarker();
    }
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    DrawableCompat.setTint(vectorDrawable, tintColor);
    vectorDrawable.draw(canvas);
    return BitmapDescriptorFactory.fromBitmap(bitmap);
}

之后,它只剩下imageview.setImageBitmap(Bitmapdescriptor(参数);


0
投票

通过阅读注释,您的产品对象看起来像将图像存储为字符串。 getImage()方法必须返回int。

在Product中声明图像(带有一些默认图像),如下所示:

private int image = R.drawable.default_image;

吸气剂看起来像这样

public int getImage() {
  return image;
}

0
投票
// Object class for Product

class Product{

        public Product(String title, String short_description, double rating, double price, int image) {
            this.title = title;
            this.short_description = short_description;
            this.rating = rating;
            this.price = price;
            this.image = image;
        }

        String title;
        String short_description;
        double rating;
        double price;
        int image;
    }


    //Array List of products
    List<Product> productList = new ArrayList<>();
          productList.add(new Product("Title1", "Short Description1", 4, 50, R.drawable.ic_thumb_down));
        productList.add(new Product("Title2", "Short Description2", 3, 100, R.drawable.ic_thumb_down));
        productList.add(new Product("Title3", "Short Description3", 4, 150, R.drawable.ic_thumb_down));




    //Adapter

        public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{

            List<Integer> resourceList;

            public ImageAdapter(List<Integer> resourceList) {
                this.resourceList = resourceList;
            }

            @NonNull
            @Override
            public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                return new ImageViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_image, viewGroup, false));
            }

            @Override
            public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {


              Product product = productList.get(i);
              imageViewHolder.imageView.setImageResource(product.image);
              // Here you can also use other field like title, description, etc... 

            }

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

            class ImageViewHolder extends RecyclerView.ViewHolder{

                ImageView imageView;

                public ImageViewHolder(@NonNull View itemView) {
                    super(itemView);
                    imageView = itemView.findViewById(R.id.imageView3);
                }
            }

        }

0
投票

尝试使用:

holder.imageView.setImageDrawable(ContextCompact.getDrawable(ctxt, R.drawable.YOUR_RESOURCE_IMAGE));

如果你的product.getimage()返回一个资源ID,它是R.drawable.YOUR_RESOURCE_IMAGE,请尝试使用:

  holder.imageView.setImageDrawable(ContextCompact.getDrawable(ctxt, product.getimage()));
© www.soinside.com 2019 - 2024. All rights reserved.