По нажатию кнопок true/false нужно вывести надпись correct/incorrect и перелистнуть на следующий вопрос одновременно а по нажатию на back перелистнуть на предыдущий вопрос. До использования ViewModel все было нормально но теперь нужно дважды нажать на кнопку чтобы случилось действие. Как быть? В чем моя ошибка?
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var trueButton: Button
private lateinit var falseButton: Button
private lateinit var backButton: Button
/*nextButton*/
private lateinit var questionTextView: TextView
private var k = 0
private fun checkAnswer(userAnswer: Boolean) {
val correctAnswer = quizViewModel.currentQuestionAnswer
val messageResId = if (userAnswer == correctAnswer) {
k+=1
R.string.correct_toast
} else {
R.string.incorrect_toast
}
Toast.makeText(
this, messageResId,
Toast.LENGTH_SHORT
).apply { setGravity(Gravity.TOP, 0, 0) }.show()
}
private fun result(){
if(quizViewModel.currentIndex==quizViewModel.f) {
var str = "Your result is $k out of ${quizViewModel.c}"
Toast.makeText(
this, str,
Toast.LENGTH_SHORT
).show()
}
}
private val quizViewModel: QuizViewModel by lazy{
val factory = QuizViewModelFactory()
ViewModelProvider(this, factory).get(QuizViewModel::class.java)
}
@SuppressLint("MissingInflatedId")
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
Log.i(TAG,
"onSaveInstanceState")
savedInstanceState.putInt(KEY_INDEX,
quizViewModel.currentIndex)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(TAG, "onCreate(Bundle?) called")
setContentView(R.layout.activity_main)
Log.d(TAG, "Got a QuizViewModel:$quizViewModel")
val currentIndex = savedInstanceState?.getInt(KEY_INDEX, 0) ?: 0
quizViewModel.currentIndex = currentIndex
trueButton = findViewById(true_button)
falseButton = findViewById(false_button)
backButton = findViewById(back_button)
/*nextbutton*/
fun updateQuestion() {
val questionTextResId = quizViewModel.currentQuestionText
questionTextView.setText(questionTextResId)
}
fun backQuestion(){
val questionTextResIdBack = quizViewModel.currentQuestionText
questionTextView.setText(questionTextResIdBack)
}
questionTextView = findViewById(question_text)
trueButton.setOnClickListener { view: View ->
checkAnswer(true)
updateQuestion()
quizViewModel.moveToNext()
result()
}
falseButton.setOnClickListener { view: View ->
checkAnswer(false)
updateQuestion()
quizViewModel.moveToNext()
result()
}
backButton.setOnClickListener { view: View ->
if(quizViewModel.currentIndex>=0){
quizViewModel.moveToPrevious()
backQuestion()
}
}
val questionTextResId = quizViewModel.currentQuestionText
questionTextView.setText(questionTextResId)
}
...
QuizViewModel
...
private const val TAG = "QuizViewModel"
class QuizViewModel: ViewModel() {
var currentIndex = 0
private val questionBank = listOf(
Question(R.string.question_australia,
true),
Question(R.string.question_oceans,
true),
Question(R.string.question_mideast,
false),
Question(R.string.question_africa,
false),
Question(R.string.question_americas,
true),
Question(R.string.question_asia, true)
)
val currentQuestionAnswer: Boolean
get() = questionBank[currentIndex].answer
val currentQuestionText: Int
get() = questionBank[currentIndex].textResId
fun moveToNext() {
currentIndex = (currentIndex + 1) % questionBank.size
}
fun moveToPrevious() {
currentIndex = (currentIndex - 1) % questionBank.size
}
val f = questionBank.size-1
val c = questionBank.size
}