Reputation: 2679
I have a Kotlin data class with around 37 attributes/parameters. I'd like to get the values of all those parameters into a list. How can I do that in a clean, minimal and efficient way? I've tried searching, but haven't come across anything of the sort yet.
Any help would be appreciated. Thanks.
Edit
Each of the attributes/parameters is a string. I'm loading each of the strings in its own TextView in Android (Yes, 37 TextViews in a TableLayout each with their own labels. Kind of what a receipt would look like). But I don't want to do this (textView.text = myClass.parameter
) 37 times.
Upvotes: 2
Views: 10313
Reputation: 4184
Include kotlin-reflect
// Gradle Groovy DLS
implementation "org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}"
// Gradle Kotlin DLS
implementation(kotlin("reflect"))
Suppose the following case
data class DataClass(val a: String, val b: String, val c: String, val d: String, val e: String)
val instance = DataClass("A", "B", "C", "D", "E")
DataClass::class.memberProperties.forEach { member ->
val name = member.name
val value = member.get(instance) as String
findTextViewByName(name).text = value
}
It's up to you to implement findTextViewByName
function
Upvotes: 9