How to callback a view created with an ID in Kotlin

275 views Asked by At

My current scripts are

fun addRow(text: String, rowNum: Int){
    val textViewNm: TextView = TextView(this.context)
    textViewNm.text = text
    textViewNm.gravity = Gravity.CENTER
    textViewNm.id = rowNum
    textBox.addView(textViewNm)
}

I'd like to callback and control textViews made by "addRow" function.

However I don't know how to call them back with ID (textViewNm.id = rowNum).

nothing showed when I tried

textViewNm.1 ,2 ,3 // which was rowNum
1

There are 1 answers

2
a_local_nobody On BEST ANSWER
textViewNm.1 ,2 ,3 // which was rowNum

this won't work, because there's no property on a textView which is called 1, 2 or 3, etc.

that would be the same as using

textViewNm.SomeRandomPropertyWhichDoesNotExist

you should probably make use of another method to find these views, perhaps using findViewById or finding by tag


you could also consider keeping these dynamic components inside an array list and then iterating over this list