【问题标题】:Load Image with Picasso from drawable using Path使用路径从可绘制对象中加载带有毕加索的图像
【发布时间】:2021-05-21 20:55:35
【问题描述】:

我想做的是从drawable加载一些图像。事实上,我想加载像R.drawable."string" 这样的图像,但不幸的是找不到将字符串放入其中的方法。此外,字符串是从 MySQL 数据库加载的。

这里你可以看到代码。

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private Context mContext;
    private ArrayList<Product> mProducts;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public ProductAdapter(Context context, ArrayList<Product> products) {
        mContext = context;
        mProducts = products;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.product, parent, false);
        return new ProductViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        Product currentProduct = mProducts.get(position);
        String name = currentProduct.getName();
        String photo = currentProduct.getPhoto();
        double price = currentProduct.getPrice();

        holder.textViewName.setText(name);
        holder.textViewPrice.setText(price + "€");

        int id = mContext.getResources().getIdentifier(photo, "drawable", mContext.getPackageName());



        Picasso.get().load(id).into(holder.imageView);
    }

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

    public class ProductViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        public TextView textViewName;
        public TextView textViewPrice;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_view);
            textViewName = itemView.findViewById(R.id.textViewName);
            textViewPrice = itemView.findViewById(R.id.textViewPrice);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            mListener.onItemClick(position);
                        }
                    }
                }
            });

        }
    }
}

如您所见,我尝试了使用谷歌搜索发现的 int id = mContext.getResources().getIdentifier(photo, "drawable", mContext.getPackageName());,但似乎不起作用...

非常感谢您提供的任何帮助。

【问题讨论】:

  • 您是否尝试从应用的内部存储中加载图像??
  • 是的,图片已经加载到drawable文件夹中
  • 我想这就是你要找的stackoverflow.com/questions/6677804/…
  • 这实际上很有帮助。我会检查一下。谢谢@Praveen

标签: java android android-drawable picasso


【解决方案1】:

我会建议另一种方法,而不是您需要使用的方法:您可以对从 MySQL 数据库返回的内容设置条件,并相应地返回 id (R.drawable.xx),如下所示:

int id;

if(photo.equals("foo")) {
    id = R.drawable.foo;

} else if(photo.equals("bla")) {
    id = R.drawable.bla;

} else {
    id = // default id
}

Picasso.get().load(id).into(holder.imageView);

因此,这样每个可绘制对象在数据库中都有一个特定的映射。

【讨论】:

  • 我可以按照这种方法处理 100 张图片吗?
  • @Koti 不管性能如何,是的,你可以......但你可以用不同的方式来制作它,比如在一个数组中添加所有可绘制对象,并且只将索引保存在数据库中,所以你现在不需要有条件的 .. 您可能会考虑的另一点;在drawables中添加100个图像以在它们之间切换并不是那么好;它们可能在本地存储或在线仓库中
  • 好的,有什么不同的方法可以使用 Picasso 加载图像而不将它们放入可绘制对象中?
  • 您可以load images from internal storage 或通过互联网使用Picasso.get().load(imageUrl).into(imageView);
猜你喜欢
  • 1970-01-01
  • 2019-07-15
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
相关资源
最近更新 更多