Home > database >  screen is moving to next question even answer is wrong
screen is moving to next question even answer is wrong

Time:04-19

I'm coding a quiz app, the main problem here is when an option is clicked question is changed I want that if option is wrong question remain same option become red..

class Level1 : AppCompatActivity(),View.OnClickListener {
    lateinit var binding: ActivityLevel1Binding
    var mQuestionList=QuestionsAndAnswer.getQuestions()
    lateinit var imageView: ImageView

    private var mCurrentPosition: Int = 0 // this question in model class//


    private var mSelectedOptionPostion: Int = 0  // this is current option where user clicked for answer///
    lateinit var progress:ProgressBar
    private lateinit var  tvOptionOne:TextView
    private lateinit var tvOptionTwo:TextView
    private lateinit var tvOptionThree:TextView
    private lateinit var tvOptionFour:TextView
    lateinit var progressBar: ProgressBar
    lateinit var tvProgressBar: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLevel1Binding.inflate(layoutInflater)
        setContentView(binding.root)
        mQuestionList=QuestionsAndAnswer.getQuestions()
        setQuestion()
        binding.tvOptionFour.setOnClickListener(this)
        binding.tvOptionThree.setOnClickListener (this)
        binding.tvOptionTwo.setOnClickListener (this)
        binding.tvOptionOne.setOnClickListener (this)
        binding.progressBar
    }
    private fun setQuestion() {
        setDefault()
        imageView=binding.quesImage
        binding.apply {
            val questions= mQuestionList[mCurrentPosition]
            progressBar.progress = mCurrentPosition
            tvProgressBar.text = "$mCurrentPosition"   "/"   progressBar.max
            binding.quesImage.setImageResource(questions.image)
            tvOptionOne.text = questions.optionOne
            tvOptionTwo.text = questions.optionTwo
            tvOptionThree.text = questions.optionThree
            tvOptionFour.text = questions.optionFour
            }
    }
    override fun onClick(v: View?) {
        when(v?.id){
            R.id.tvOptionOne->{
                Selected(1,binding.tvOptionOne)
                if( mCurrentPosition != mQuestionList.size-1){
                    mCurrentPosition   
                    setQuestion()
                    val questions=mQuestionList?.get(mCurrentPosition -1)
                    if (questions?.correctAnswer!=mSelectedOptionPostion){
                        AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
                    }
                    else {
                        AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)

                    }
                }
                else {
                    val intent=Intent(this@Level1,ScoringActivity::class.java)
                    startActivity(intent)
                    finish()
                }
            }
            R.id.tvOptionTwo->{
               Selected(2,binding.tvOptionTwo)
                if( mCurrentPosition != mQuestionList.size-1){
                    mCurrentPosition   
                    setQuestion()
                    val questions=mQuestionList?.get(mCurrentPosition -1)
                    if (questions?.correctAnswer!=mSelectedOptionPostion){
                        AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
                        mCurrentPosition
                        Toast.makeText(this,"wrong answer",Toast.LENGTH_SHORT).show()
                    }
                    AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
                }
                else {

                        val intent=Intent(this@Level1,ScoringActivity::class.java)
                        startActivity(intent)
                        finish()
                }
            }
            R.id.tvOptionThree->{
                Selected(3,binding.tvOptionThree)
                if( mCurrentPosition != mQuestionList.size-1){
                    mCurrentPosition   
                    setQuestion()
                    val questions=mQuestionList?.get(mCurrentPosition -1)
                    if (questions?.correctAnswer!=mSelectedOptionPostion){
                        AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
                     Toast.makeText(this,"wrong answer",Toast.LENGTH_SHORT).show()
                    }
                    else{
                        AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)

                    }
                }
                else {
                    val intent=Intent(this@Level1,ScoringActivity::class.java)
                    startActivity(intent)
                    finish()
                }
            }

// at last question on wrong answer click no action happens like no wrong answer drawable file is called //


            R.id.tvOptionFour->{
               Selected(4,binding.tvOptionFour)
                if( mCurrentPosition != mQuestionList.size-1){
                    mCurrentPosition   
                    setQuestion()
                    var questions=mQuestionList?.get(mCurrentPosition -1)
                    if (questions?.correctAnswer!=mSelectedOptionPostion){
                        AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
                    }
                    else {
                        AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
                    }
                }
                else {
                    val intent=Intent(this@Level1,ScoringActivity::class.java)
                    startActivity(intent)
                    finish()
                }
            }
        }
    }


// here answers are maatched //


        private fun AnswerView(answer: Int, drawAbleView: Int) {
        when (answer) {
            1 -> {
                binding.tvOptionOne.background = ContextCompat.getDrawable(this, drawAbleView)
            }
            2 -> {
                binding.tvOptionTwo.background = ContextCompat.getDrawable(this, drawAbleView)
            }
            3 -> {
                binding.tvOptionThree.background = ContextCompat.getDrawable(this, drawAbleView)
            }
            4 -> {
                binding.tvOptionFour.background = ContextCompat.getDrawable(this, drawAbleView)
            }
        }
    }
    private fun Selected(selected: Int, tv: TextView) {
        setDefault()
        mSelectedOptionPostion = selected
        tv.setTextColor(Color.parseColor("#FF03DAC5"))
        tv.setTypeface(tv.typeface, Typeface.BOLD)
        tv.background = ContextCompat.getDrawable(this,
            R.drawable.selected_tex_view)
    }
    private fun setDefault() {
        val options = ArrayList<TextView>()
        options.add(0, binding.tvOptionOne)
        options.add(1, binding.tvOptionTwo)
        options.add(2, binding.tvOptionThree)
        options.add(3, binding.tvOptionFour)
        for (option in options) {
            option.setTextColor(Color.parseColor("#FF03DAC5"))
            option.typeface = Typeface.DEFAULT
            option.background = ContextCompat.getDrawable(this, R.drawable.round_text_view)
        }
    }
}

CodePudding user response:

Try to change your onClick logic like this:

Note that I've declared a couple of methods to avoid code duplication

override fun onClick(v: View?) {
    when(v?.id){
        R.id.tvOptionOne->{
            Selected(1, binding.tvOptionOne)
            if(mCurrentPosition < mQuestionList.size){
                setQuestion()
                checkQuestion()
            }
            else startScoreActivity()
        }
        R.id.tvOptionTwo->{
            Selected(2,binding.tvOptionTwo)
            if(mCurrentPosition < mQuestionList.size){
                setQuestion()
                checkQuestion()
            }
            else startScoreActivity()
        }
        R.id.tvOptionThree->{
            Selected(3,binding.tvOptionThree)
            if( mCurrentPosition < mQuestionList.size){
                setQuestion()
                checkQuestion()
            }
            else startScoreActivity()
        }
        R.id.tvOptionFour->{
           Selected(4,binding.tvOptionFour)
            if( mCurrentPosition < mQuestionList.size) {
                setQuestion()
                
            }
            else startScoreActivity()
        }
    }
}

private fun checkQuestion() {
    var questions=mQuestionList?.get(mCurrentPosition)
    if (questions?.correctAnswer!=mSelectedOptionPostion){
        AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
    }
    else {
        AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
        mCurrentPosition  
    }
}

private fun startScoreActivity() {
    val intent=Intent(this@Level1,ScoringActivity::class.java)
    startActivity(intent)
    finish()
}

CodePudding user response:

I guess you should try moving

if (questions?.correctAnswer!=mSelectedOptionPostion) {              
    AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
} else {
    mCurrentPosition   
    setQuestion()
    AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
}
  • Related