So I have a kotlin data class (model) representing a JSON object (for retrofit's GSON converter), and I want to iterate through my model in a certain order for further creating a listView of JSON data.
The Model has nesting with ArrayLists.
Model.kt
object Model {
data class Result(
val results: ArrayList<Results>?,
val syllables: Syllables,
...)
data class Results(
val definition: String,
val partOfSpeech: String,
val synonyms: ArrayList<String>?,
val antonyms: ArrayList<String>?,
...)
I have tried to do some reflections but didn't realize how to iterate through Results ArrayList. Something like that (error occurs: ArrayList can't be cast to Model.Results):
class CallOrder(private val obj: Model.Result) {
fun getOrder(): List<String> {
val result = ArrayList<String>()
for (prop in Model.Result::class.declaredMemberProperties) {
if (prop.name == "results")
for (prop1 in Model.Results::class.declaredMemberProperties)
result.add("${prop1.name} = ${prop1.get(prop.get(this.obj) as Model.Results)}")
}
return result.subList(0, 1)
}
}
I want something to call like that:
CallOrder(obj, property1, property2, property3.property31).next() // returns obj.property1
CallOrder(obj, "property1", "property2", "property3.property31").next() // or to do the same with string representations (of attribute path)
Any ideas to implement that?
Aucun commentaire:
Enregistrer un commentaire