【问题标题】:How to apply random list of numbers for selection from array to select image?如何应用随机数字列表从数组中选择以选择图像?
【发布时间】: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


【解决方案1】:

如果您想在每次点击时选择一个新的随机图像,您只需要这样做:

val array = intArrayOf(R.drawable.cat1, R.drawable.cat2, R.drawable.cat3, R.drawable.cat4, R.drawable.cat5)


imageView.setOnClickListener {
    imageView.setImageResource(array.random())
}

如果您绝对需要使用预定义的随机值列表(有什么意义?),那么您需要跟踪您使用的最后一个索引。类似的东西:

// in your class
var lastIndex = 0
val randomValues = List(15) { Random.nextInt(0, 5) }

// setting the listener
val array = intArrayOf(R.drawable.cat1, R.drawable.cat2, R.drawable.cat3, R.drawable.cat4, R.drawable.cat5)

imageView.setOnClickListener {
    imageView.setImageResource(array[randomValues[lastIndex]])
    lastIndex = (lastIndex + 1) % randomValues.size
}

【讨论】:

  • 谢谢,但这并不是我想要的。如我所见,您使用了 randomValues 的元素数量,但实际上数组中的元素是按顺序选择的(它们不是随机创建的)。 “重点是什么?” - 见链接的帖子。我在检查图像时遇到延迟问题,所以我想预先创建随机列表并检查该列表以获得所有正确的答案。如果您知道我如何解决该问题,我将不胜感激。
  • 抱歉,出错了。我将代码从 array[lastIndex] 更正为 array[randomValues[lastIndex]]
【解决方案2】:

如果您只想从数组中选择一个随机元素,您可以使用 Array.random() 方法,该方法只从数组中返回一个随机元素

var array = intArrayOf(R.drawable.cat1,R.drawable.cat2,R.drawable.cat3,R.drawable.cat4,R.drawable.cat5)
imageView.setImageResource(array.random())

编辑

如果您想根据随机生成的索引列表选择资源列表,您可以通过将每个索引转换为正确的资源来实现此目的。然后您可以使用forEach 方法对每个选定的项目执行操作:

var array = intArrayOf(R.drawable.cat1,R.drawable.cat2,R.drawable.cat3,R.drawable.cat4,R.drawable.cat5)
val randomValues = List(15) { Random.nextInt(0, 5) }

randomValues
    .map { array[it] }
    .forEach { imageView.setImageResource(it) }

基本上,您的方法失败了,因为您尝试将整个 randomValues 列表用作单个索引值。相反,您应该以一种或另一种方式遍历列表,并为每个随机生成的数字选择资源。

【讨论】:

  • 感谢您的回答,但我想先创建列表,然后按该列表的顺序使用。例如,如果列表是1,3,9,7,并且我在setOnClickListener() 中使用它,则每次点击它都应该按顺序给我图像1,3,9,7
  • 如果您好奇我为什么要这样做,请查看this。我的目标是在测验前设置问题的值。
  • @vasik988 ImageView.setImageResource() 只接受一个参数,您是否有多个ImageView 或想使用一个并根据索引列表将每个图像替换为新图像?
  • 根据索引列表将每个图像替换为新图像
  • 好的,但我不太明白如何在setOnClickListener() 中使用randomValues .map { array[it] } .forEach { imageView.setImageResource(it) }。如果我按下 imageView,没有任何变化。
猜你喜欢
  • 2013-03-27
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多