class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton: Button = findViewById(R.id.roll_button)
rollButton.setOnClickListener { clickMe() }
}
private var word = listOf("Bla", "Blabla", "Yo", "Go")
private fun clickMe() {
val resultText: TextView = findViewById(R.id.result_text)
val listIndex = 0
resultText.text = word[listIndex]
listIndex + 1
}
}
Sorry for this stupid question, I mean to show "Bla" on first click, "Blabla" on second click and so on. but when I run the app, the screen only shows "Bla" even on second or next click. Thanks in advance
solved 1
j
jasu
01/31/2022, 6:49 AM
1. keep
listIndex = 0
outside of clickMe()
2. have a condition to reset listIndex inside
clickMe()
3. reAssign listIndex with increased index
Copy code
val listIndex = 0
private fun clickMe() {
if (listIndex + 1 > word.size)
listIndex = 0
val resultText: TextView = findViewById(R.id.result_text)
resultText.text = word[listIndex]
listIndex = listIndex + 1
}