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

时间:2018-09-13 03:35:02

标签: android android-studio android-recyclerview recycler-adapter

这是Product Bean类enter image description here

所以我的Recycle视图正在工作,但是这行代码不起作用。这是从“我的适配器类”获得的。

SELECT *
FROM(
    SELECT OPP_ID,PRJ_ID,
        ROW_NUMBER() OVER (PARTITION BY OPP_ID ORDER BY MAX(CREATION_DATE) DESC) AS RN
    FROM OPPOR
    GROUP BY OPP_ID,PRJ_ID
    ORDER BY MAX(CREATION_DATE) DESC) OP
WHERE OP.RN = 1

CLASS

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

7 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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);

您可以使用滑行和毕加索

如果您获得可绘制的图像表格,请设置图像

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

答案 2 :(得分:0)

我认为您获得了产品对象的字符串,则应尝试将estring转换为Uri或位图,以便在imageview中进行设置时可以显示,如果显示您的产品,它将很有帮助知道什么类型的值返回product.getimage();的类  Uri.parse(product.getImage());

答案 3 :(得分:0)

我有一种加载可绘制文件的方法,它们是向量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(parameters);

答案 4 :(得分:0)

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

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

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
  -H "Accept: application/json" \
  -H "Accept-Language: en_US" \
  -u "client_id:secret" \
  -d "grant_type=client_credentials"

吸气剂将如下所示

private int image = R.drawable.default_image;

答案 5 :(得分: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);
                }
            }

        }

答案 6 :(得分:0)

尝试使用:

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

如果您的product.getimage()返回的资源ID是R.drwable.YOUR_RESOURCE_IMAGE 尝试使用:

  holder.imageView.setImageDrawable(ContextCompact.getDrawable(ctxt, product.getimage()));