【发布时间】:2020-04-18 19:25:15
【问题描述】:
如何制作随机数列表并使用它来设置数组中的图像?
val randomValues = List(15) { Random.nextInt(0, 5) }
var array = intArrayOf(R.drawable.cat1,R.drawable.cat2,R.drawable.cat3,R.drawable.cat4,R.drawable.cat5)
imageView.setImageResource(array[randomValues])
我在imageView.setImageResource(array[randomValues]) 中的randomValues 上遇到类型不匹配。 Required: Int and Found: List <int>.
已编辑
val randomValues = List(15) { Random.nextInt(0, 5) }
var array = intArrayOf(R.drawable.cat1,R.drawable.cat2,R.drawable.cat3,R.drawable.cat4,R.drawable.cat5)
imageView.setOnClickListener {
randomValues
.map { array[it] }
.forEach { imageView.setImageResource(it) }
}
【问题讨论】:
-
问题不清楚。你想让我做什么?如果您想从数组中选择一个随机图像,则不需要列表,而只需要一个数字:
imageView.setImageResource(array[Random.nextInt(0, 5)])。 -
不,我不希望它是随机的。我想按特定顺序从预先创建的列表中使用,例如 this 列表。
标签: android arrays list kotlin random