Kiki Abdullah
01/31/2022, 5:59 AMclass 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 advancejasu
01/31/2022, 6:49 AMlistIndex = 0
outside of clickMe()
2. have a condition to reset listIndex inside clickMe()
3. reAssign listIndex with increased index
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
}
Kiki Abdullah
01/31/2022, 8:36 AMlistIndex
to var in order to reassign it in clickMe()
jasu
01/31/2022, 8:40 AM