gpt4 book ai didi

android - 将图像资源设置为 RecyclerView 列表项

转载 作者:行者123 更新时间:2023-12-04 23:58:11 25 4
gpt4 key购买 nike

我正在尝试为我的 RecyclerView 中的每个 CardView 项目设置一个图像。

这是我的onBindViewHolder:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Masar masar=masrList.get(position);
holder.masarName.setText(masar.getMasarTitle());
holder.masarDesc.setText(masar.getMasarDescreption());

// why i'm getting error here
holder.masarImg.setImageDrawable(masar.getMasarImg());
}

这是我的类模型:

public class Masar {
public String masarTitle;
public String masarDescreption;
public int masarImg;

public Masar(String masarTitle, String masarDescreption) {
this.masarTitle = masarTitle;
this.masarDescreption = masarDescreption;
}

public Masar(String masarTitle, String masarDescreption, int masarImg) {
this.masarTitle = masarTitle;
this.masarDescreption = masarDescreption;
this.masarImg = masarImg;
}

public String getMasarTitle() {
return masarTitle;
}

public String getMasarDescreption() {
return masarDescreption;
}

public int getMasarImg() {
return masarImg;
}

public void setMasarImg(int masarImg) {
this.masarImg = masarImg;
}
}

你能告诉我为什么会出错吗?

正确的方法是什么?

最佳答案

而不是使用 holder.masarImg.setImageDrawable(masar.getMasarImg());

使用

holder.masarImg.setImageDrawable(getResources().getDrawable(masar.getMasarImg()));

holder.masarImg.setImageResource(masar.getMasarImg());

注意:要获取 getResources() 使用 context

关于android - 将图像资源设置为 RecyclerView 列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48724174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com