I'm learning Kotlin from Java, and I'm trying to figure out how to create and store the ViewModel. Storing it outside the main activity will cause it to crash, but storing it inside the main activity means my private variables cannot access them, so I get an error.
Here's the code I have
class MainActivity : AppCompatActivity() {
private lateinit var trueButton: Button
private lateinit var falseButton: Button
private lateinit var nextButton: Button
private lateinit var prevButton: Button
private lateinit var questionTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val quizViewModel: QuizViewModel = ViewModelProvider(this).get(QuizViewModel::class.java)
trueButton = findViewById(R.id.true_button)
falseButton = findViewById(R.id.false_button)
nextButton = findViewById(R.id.next_button)
prevButton = findViewById(R.id.back_button)
questionTextView = findViewById(R.id.question_text)
trueButton.setOnClickListener {
checkAnswer(true)
}
falseButton.setOnClickListener {
checkAnswer(false)
}
nextButton.setOnClickListener {
quizViewModel.moveToNext()
updateQuestion()
}
prevButton.setOnClickListener {
quizViewModel.moveToBack()
updateQuestion()
}
updateQuestion()
}
private fun checkAnswer(userAnswer: Boolean){
val correctAnswer = quizViewModel.currentQuestionAnswer
val messageResId = if (userAnswer == correctAnswer) {
R.string.correct_toast
} else {
R.string.incorrect_toast
}
Toast.makeText(
this,
messageResId,
Toast.LENGTH_SHORT
).show()
}
private fun updateQuestion() {
val questionTextResID = quizViewModel.currentQuestionText
questionTextView.setText(questionTextResID)
}
}
Unfortunately, putting the ViewModel inside my onCreate means I can't access them in my functions, namely checkAnswer and updateQuestion.
import androidx.lifecycle.ViewModel
class QuizViewModel : ViewModel() {
private val questionBank = listOf(
Question(R.string.question_aus, true),
Question(R.string.question_ocean, true),
Question(R.string.question_mideast, false),
Question(R.string.question_africa, false),
Question(R.string.question_america, true),
Question(R.string.question_asia, true)
)
var currentIndex = 0
val currentQuestionAnswer: Boolean
get() = questionBank[currentIndex].answer
val currentQuestionText: Int
get() = questionBank[currentIndex].textResId
fun moveToNext(){
currentIndex = (currentIndex 1) % questionBank.size
}
fun moveToBack(){
currentIndex = (currentIndex - 1) % questionBank.size
}
}
And my error
e: C:\Users\Jacob\AndroidStudioProjects\GeoQuiz\app\src\main\java\com\jacobjumper\geoquiz\MainActivity.kt: (54, 29): Unresolved reference: quizViewModel
CodePudding user response:
just move the quizViewModel
to the rest of the variables.
so like
class MainActivity : AppCompatActivity() {
private lateinit var trueButton: Button
private lateinit var falseButton: Button
private lateinit var nextButton: Button
private lateinit var prevButton: Button
private lateinit var questionTextView: TextView
private lateinit var quizViewModel: QuizViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
quizViewModel = ViewModelProvider(this).get(QuizViewModel::class.java)
CodePudding user response:
Make sure you have added all dependency listed here