Java 使用路径从drawable加载毕加索的图像
我想做的是从drawable加载一些图像。事实上,我想加载像Java 使用路径从drawable加载毕加索的图像,java,android,android-drawable,picasso,Java,Android,Android Drawable,Picasso,我想做的是从drawable加载一些图像。事实上,我想加载像R.drawable。“string”这样的图像,但不幸的是,我找不到一种方法将字符串放入其中。此外,字符串是从MySQL数据库加载的 在这里你可以看到代码 public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> { private Context mContext; private
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);
}
}
}
});
}
}
}
公共类ProductAdapter扩展了RecyclerView.Adapter{
私有上下文;
私有ArrayList MPProducts;
私人监听者;
公共接口侦听器{
无效单击(内部位置);
}
公共void setOnItemClickListener(OnItemClickListener侦听器){
mListener=监听器;
}
公共产品适配器(上下文、ArrayList产品){
mContext=上下文;
MPProducts=产品;
}
@非空
@凌驾
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图v=LayoutFlater.from(mContext)。充气(R.layout.product,父项,false);
返回新产品视图持有者(v);
}
@凌驾
public void onBindViewHolder(@NonNull ProductViewHolder holder,int位置){
Product currentProduct=MPProducts.get(位置);
字符串名称=currentProduct.getName();
字符串photo=currentProduct.getPhoto();
double price=currentProduct.getPrice();
holder.textViewName.setText(名称);
holder.textViewPrice.setText(价格+“€”);
int id=mContext.getResources().getIdentifier(照片,“可绘制”,mContext.getPackageName());
Picasso.get().load(id).into(holder.imageView);
}
@凌驾
public int getItemCount(){
返回mpProducts.size();
}
公共类ProductViewHolder扩展了RecyclerView.ViewHolder{
公共影像视图;
公共文本视图文本视图名称;
公共文本视图文本视图价格;
public ProductViewHolder(@NonNull View itemView){
超级(项目视图);
imageView=itemView.findViewById(R.id.image\u视图);
textViewName=itemView.findViewById(R.id.textViewName);
textViewPrice=itemView.findViewById(R.id.textViewPrice);
itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
if(mListener!=null){
int position=getAdapterPosition();
如果(位置!=RecyclerView.无位置){
姆利斯滕纳·奥尼麦克利克(职位);
}
}
}
});
}
}
}
如您所见,我尝试了intid=mContext.getResources().getIdentifier(照片,“drawable”,mContext.getPackageName())代码>我在谷歌上找到的,但似乎不起作用
非常感谢您提供的任何帮助。我建议使用另一种方法,而不是您需要使用的方法:您可以对从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);
因此,通过这种方式,每个drawable在数据库中都有一个特定的映射。您是否试图从应用程序的内部存储中加载图像?是的,图像已经加载到drawable Folder中。我认为这就是您正在寻找的,它实际上很有用。我去看看。谢谢@PraveenCan我用这种方法拍摄了大约100张图片?@Koti不管性能如何,是的,你可以。。但您可以通过不同的方式来实现,比如在一个数组中添加所有Drawable,并且只在数据库中保存索引,所以现在不需要有条件。。你可能考虑的另一点;在drawables中添加100个图像以便在它们之间切换不是很好;它们可能在本地存储或在线repoOk中,我可以使用毕加索加载图像,而不将它们放在绘图设备中,这有什么不同的方式呢?您可以使用Picasso.get().load(imageUrl).into(imageView)代码>