【问题标题】:Kotlin - Get new index of item in shuffled listKotlin - 获取随机列表中项目的新索引
【发布时间】:2020-10-29 21:22:47
【问题描述】:

我有一个多项选择测验,每个答案有 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
        }
    }

数据类

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)
            }
        }
    }

【问题讨论】:

    标签: android kotlin arraylist shuffle


    【解决方案1】:

    我真的建议只创建一个这样的数据类:

    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)
    

    (注意可选参数是最后的,你不需要指定它们,因为它们默认为空)

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

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

    【讨论】:

    • 感谢您的回复。考虑到我写的问题的数量,我希望避免重写以拆分为单独的类。我可以做类似data class QuestionOption(val question:String, val answer1: String, val isCorrect = false, val answer2: String, val isCorrect = true, val answer3: String, val isCorrect = false, val answer4: String, val isCorrect = false) 的事情吗
    • 我建议仍然创建数据类,但如果您担心代码过多,您可以创建一个以简短方式自动创建问题的函数。查看编辑了解更多信息
    • 添加了另一种方法来解决它,仍然会实现更短的 Questions3 类
    • 根据您的编辑更改代码会产生错误!= cannot be applied to 'String' and 'Int'。将字符串转换为 int 会使应用程序崩溃(请参阅我的编辑)。
    • 您应该将所选文本与正确答案的文本进行比较。含义 questions3!!.correctAnswer == selectedAnswerText
    猜你喜欢
    • 2018-08-20
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 2013-04-29
    • 2019-01-25
    相关资源
    最近更新 更多