This isn't the best way to do things, but it's probably the most universal, while avoiding creating a pre-defined array of TextViews:
val base = "TextView_0"
for (i in 1 until 6) {
val textView = findViewById(resources.getIdentifier("${base}i", "id", packageName)
textView.text = something
}
I changed your for loop a little bit, since you had the wrong syntax. I also replaced ..
with until
, since ..
means through the right bound, which probably isn't what you want. If you do need 6
to be a value of i
, then change it back to ..
.
If all the TextViews are under a single parent in XML, give that parent an ID, then loop through its children:
val parent = findViewById(R.id.tvParent)
for (i in 0 until parent.getChildCount()) {
(container.getChildAt(i) as TextView).text = something
}