Reputation: 1
Sorry if this is super basic, I'm just getting started with programming in general.
So I have this list, I've managed to display an object from it randomly when the user clicks on a button.
Here is the code that does this:
val listOfElement = listOf<String>(
"test", "test2", "test 3")
fun generateElement (view: View) {
tvDisplay.text = listOfElement.random()
}
It works fine but the problem is that an object (e.g "test3") can be displayed twice before it goes through the rest of the list.
So basically I'd like to:
Thanks again and sorry if this not appropriate - it's my first time on stackoverflow!
Upvotes: 0
Views: 110
Reputation: 29320
You can make a copy of your list and them remove the random element, then repopulate the list when it's empty:
val listOfElement = listOf<String>(
"test", "test2", "test 3")
val copy = mutableListOf<String>()
fun generateElement (view: View) {
if (copy.isEmpty()) {
copy.addAll(listOfElement)
}
val random = copy.random()
copy.remove(random)
tvDisplay.text = random
}
Upvotes: 1