Tad
Tad

Reputation: 947

How to set a list/array to a TextView

I have a mutable list that changes each time the generate() function is called. What I'm trying to do is convert it to a string and set it to a TextView. The way I set the TextView below works for Integers but not for lists. It just does not display the contents of the list and I have no idea why it won't work. Instead the TextView2 does this: Genereated Numbers: []

    val text = findViewById<TextView>(R.id.textView)
    val text2 = findViewById<TextView>(R.id.textView2)

    var possibleInputs = mutableListOf(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)

    fun generate() {
        var num = (0..20).shuffled().last()

        when {
            possibleInputs.size == 0 -> text.text = "Done"
            num in possibleInputs -> {
                text.text = "$num"
                text2.text = "Generated Numbers: $possibleInputs"
                possibleInputs.remove(num)
            }
            else -> generate()
        }
    }

Upvotes: 3

Views: 2623

Answers (1)

Tad
Tad

Reputation: 947

This is the code I used to fix it

for (i in 0 until possibleInputs.size) {
    text2.append(possibleInputs[i].toString())
    }

Upvotes: 2

Related Questions