Kotlin - 在随机列表中获取项目的新索引

Kotlin - Get new index of item in shuffled list

我有一个多项选择测验,每个答案有 4 个选项。在包含问题和选项的 ArrayList 中,正确答案设置为正确选项的索引。我想洗牌,但不确定如何确定正确答案的新索引。有什么想法吗?

问题对象

object ConstantsAnalysis {
        const val TOTAL_CORRECT: String = "total_correct"
        const val TOTAL_OPP: String = "total_opp"
        fun getQuestions3(): ArrayList<Questions3> {
            val questionList = ArrayList<Questions3>()
            val q1 = Questions3(1, null,
                "On a graph, the horizontal line along which data are plotted is the _____",
                "y axis", "x axis", "origin", "quadrant", 2, R.string.Jones_1995, null)
questionList.addAll(listOf(q1))
            questionList.shuffle()
            return questionList
        }
    }

数据class

data class Questions3(
    val id: Int, val image: Int?, val question: String, val option1: String, val option2: String,
    val option3: String, val option4: String, val correctAnswer: Int, val dialogBox: Int?, val dialogBox2: Int?)

随机选择

val ansorder = arrayOf(question.option1, question.option2, question.option3, question.option4)
        ansorder.shuffle()
        radio_button1.text = ansorder[0]
        radio_button2.text = ansorder[1]
        radio_button3.text = ansorder[2]
        radio_button4.text = ansorder[3]

检查答案选择

if (questions3!!.correctAnswer != mSelectedOptionPosition) {
//do x
}

编辑(由于correct answer是一个字符串,并且在打乱后索引发生变化,answerView(questions3.correctAnswer, R.drawable.correct_option_border

class QuestionsActivityAnalysis : AppCompatActivity(), View.OnClickListener {

    private var mCurrentPosition:Int = 1
    private var mQuestionsList:ArrayList<Questions3>? = null
    private var mSelectedOptionPosition:Int = 0
    private var mCorrectAnswers: Int = 0
    private var mSelectedOptionText: String? = null

private fun shuffle() {
        val question = mQuestionsList!![mCurrentPosition - 1]
        val ansorder = arrayOf(question.option1, question.option2, question.option3, question.option4)
        ansorder.shuffle()
        radio_button1.text = ansorder[0]
        radio_button2.text = ansorder[1]
        radio_button3.text = ansorder[2]
        radio_button4.text = ansorder[3]
    }

override fun onClick(v: View?) {
        when(v?.id){
            R.id.radio_button1 -> { selectedOptionView(radio_button1, 1)
                mSelectedOptionText = radio_button1.text as String?
            }
            R.id.radio_button2 -> { selectedOptionView(radio_button2, 2)
                mSelectedOptionText = radio_button2.text as String?
            }
            R.id.radio_button3 -> { selectedOptionView(radio_button3, 3)
                mSelectedOptionText = radio_button3.text as String?
            }
            R.id.radio_button4 -> { selectedOptionView(radio_button4, 4)
                mSelectedOptionText = radio_button4.text as String?
            }

R.id.btn_submit -> {
val questions3 = mQuestionsList?.get(mCurrentPosition - 1)
                    if (questions3!!.correctAnswer != mSelectedOptionText) {
} else {
                        mCorrectAnswers++
                    }
                    answerView(questions3.correctAnswer, R.drawable.correct_option_border)

private fun answerView(answer: Int, drawableView: Int) {
        when(answer){
            1 -> {
                radio_button1.background = ContextCompat.getDrawable(this, drawableView)
            }
            2 -> {
                radio_button2.background = ContextCompat.getDrawable(this, drawableView)
            }
            3 -> {
                radio_button3.background = ContextCompat.getDrawable(this, drawableView)
            }
            4 -> {
                radio_button4.background = ContextCompat.getDrawable(this, drawableView)
            }
        }
    }

我真的建议像这样创建数据 class:

data class QuestionOption(val question:String, val isCorrect = false)

之后,您可以按照自己喜欢的方式随机播放,只需检查所选的 QuestionOption 是否已将 isCorrect 设置为 true。你得到了很多好处,逻辑也变得更简单了。

编辑:

为了更容易以这种方式提出问题:

一般来说,如果您在代码中添加问题,您只需要尽可能多的必要代码。为此,您可以声明一个好的构造函数或一个基本上将您的值映射到构造函数的函数。在你的情况下,我会说

data class Questions3(
    val id: Int, val question: String, val option1: String, val option2: String,
    val option3: String, val correctOption: String, val image: Int?=null,val dialogBox1: Int?=null,val dialogBox2: Int?=null)

(注意可选参数是如何排在最后的,您也不需要指定它们,因为它们默认为 null)

有道理,理论上您也可以(不太干净但很简单)只是随机选择选项 1-3 和 correctOption,然后比较 correctOption 字符串是否与所选字符串匹配。

否则,正如我所说,您始终可以创建映射内容的逻辑。在这里,您可以从 Constructor 映射到另一个 Constructor,与 return 完成的对象的函数相同。