所以我在kotlin中创建了一个数据类,我想引用一个可绘制的资源,但该数据类可能有大约40种不同的鸟。我想做的是让不同的鸟根据鸟的编号显示不同的图片。
例如:
val bird = Bird(1) //where the 1 is the bird index in the file.
bird.updatevalues() // updates the bird data (name etc.)
//what I want to do is this:
R.drawable.(bird.getDrawableName)
或者有没有一种方法可以搜索资源,找到一个与这只鸟同名的资源,然后我就可以返回ID?
例如:
data class Bird(birdNumIn: Int){
val birdNum = birdNumIn
var birdName: String
fun updateValues(){
birdName = //name comes from CSV file.
}
fun searchDrawables(){
//find the drawable resource corresponding to the birds name
}
val bird = Bird(1) //where the 1 is the bird index in the file.
bird.updatevalues() // updates the bird data (name etc.)
getDrawable(bird.searchDrawables()) //should get the photo
如果还有其他我没有想过的方式,请告诉我。这个问题困扰了我一段时间!
提前感谢。
试试这个
int id = getResources().getIdentifier(drawableImageName, "drawable", getPackageName());
这里drawableImageName
是您的动态图像名称。