Ofir Bar
02/05/2020, 9:46 AMsindrenm
02/05/2020, 9:48 AMsindrenm
02/05/2020, 9:51 AMcontainer.children
.filterIsInstance<TextView>()
.forEach {
it.text = ""
}
(Not tested, but I seem to remember the APIs are somewhat similar.)Ofir Bar
02/05/2020, 9:56 AMsindrenm
02/05/2020, 10:15 AMsindrenm
02/05/2020, 10:16 AMchildren
is an extension from AndroidX, defined as such:
val ViewGroup.children: Sequence<View>
get() = object : Sequence<View> {
override fun iterator() = this@children.iterator()
}
You'll need the core-ktx library to use it (or define it yourself, of course).Mikołaj Karwowski
02/05/2020, 11:02 AMAlowaniak
02/05/2020, 12:12 PM.text = ""
part you could do:
listOf(
apeFoo,
apeBar,
apeFoobar,
apeEtc
).forEach {
it.text = ""
}
Ofir Bar
02/05/2020, 12:36 PMAlowaniak
02/05/2020, 12:55 PMbut how do I pass the text itself dynamically?Not sure what you mean? If you want different text for each TextView you will have to set the text specifically for them
Ofir Bar
02/05/2020, 1:03 PMlistOf(
apeFoo("apeFooText"),
apeBar("apeBar Text"),
apeFoobar("Foobar is king text"),
apeEtc("Etc text")
).forEach {
it.text = it.textInConstructor
}
// We pass dynamic text yet we eliminate the constant call to ".text"
Mikołaj Karwowski
02/05/2020, 1:34 PMlistOf(
apeFoo("apeFooText"),
apeBar("apeBar Text"),
apeFoobar("Foobar is king text"),
apeEtc("Etc text")
).forEach {
it.text = it.textInConstructor
}
does exactly the same as
apeFoo.text = "apeFooText"
apeBar.text = "apeBar Text"
apeFoobar.text = "Foobar is king text"
apeEtc.text = "Etc text"
Mikołaj Karwowski
02/05/2020, 1:39 PMMikołaj Karwowski
02/05/2020, 1:52 PMfun setTitles(titlesMap: Map<TextView, String>{
map.forEach {
it.key.text = it.value
}
}
//Then
setTitles(mapOf(
tvTitle to "I titl",
tvDesc to "I descript"
))
Or use varargs
// fun setTitles(vararg titles: Pair<TextView, String>){
titles.forEach {
it.key.text = it.value
}
}
//Then
setTitles(
tvTitle to "I titl",
tvDesc to "I descript"
)
Ofir Bar
02/05/2020, 1:52 PMMikołaj Karwowski
02/05/2020, 1:59 PMOfir Bar
02/05/2020, 2:16 PMHitender Pannu
02/06/2020, 10:11 AM(findViewById<ConstraintHelper>(R.id.textViewGroup))
.referencedIds.map { findViewById<TextView>(it) }
.forEach { it.setText("") }
sindrenm
02/06/2020, 10:53 AMHitender Pannu
02/07/2020, 5:00 AM