gpt4 book ai didi

java - 如何将 R.drawable 作为参数传递,以便解析图像

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:31 26 4
gpt4 key购买 nike

我尝试为每个对象保存一个独特的图像,但出现此错误,构造函数应如何寻找它以这种方式工作?构造函数 Beer(String, int, int) 未定义

m_beer = new ArrayList<Beer>();
final Beer b1 = new Beer("Tuborg", 7, R.drawable.tuborg);
final Beer b2 = new Beer("Carlsberg", 7, R.drawable.carlsberg);
final Beer b3 = new Beer("Urquel", 9, R.drawable.urquel);


public class Beer
{
//Members
private String name;
private int color; //1 = Very dark 10 = Very light
private R.drawable icon;

//Methods
public Beer(String name, int color, R.drawable icon)
{
this.name = name;
this.color = color;
this.icon = icon;
}

public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}

public int getColor()
{
return this.color;
}
public void setColor(int color)
{
this.color = color;
}

public R.drawable getIcon()
{
return icon;
}

}

最佳答案

final Beer b1 = new Beer("Tuborg", 7,context.getResources().getDrawable(R.drawable.tuborg));

如前所述:

public Beer(String name, int color, Drawable icon)

或者您可以将 int 作为参数发送:

final Beer b1 = new Beer("Tuborg", 7, R.drawable.tuborg);

和:

public Beer(String name, int color, int icon)
{
this.name = name;
this.color = color;
this.icon = context.getResources().getDrawable(icon);
}

关于java - 如何将 R.drawable 作为参数传递,以便解析图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3313712/

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