i want to get one result from thre different list

52 views Asked by At

I have function receive List : list<json>

inside this list many list:

list1<go> 
list2<come> 
list3<leave>

Code:

fun handleResults(it: json) {  
    var storeCoupons = when(id_choice.toInt()){
        0->it.list1
        1->it.list2
        else->it.list3
    }  

    while (i in i.. storeCoupons?.size!!-1){

        var pos=storeCoupons.get(i).title
        var img = storeCoupons?.get(i)?.image
        var id=storeCoupons?.get(i)?.id
        users.add(User(id.toString()!!,pos!!, img!!))

        i++
    }
}

the problem is the variable storeCoupons his data type is changed because its receive some types data type as list1 and another time list2 i guess should i make another list and add all the list inside it ?

1

There are 1 answers

0
stan On

If I understand correctly, you're saying that list1 and list2 are different types, but..you want to use the same methods on both of them. Sounds like you need to define a base class that holds title, image, etc, and then have both lists extend from that. Then you can use them generically.

Here's an example of how to do that in Kotlin: https://www.programiz.com/kotlin-programming/inheritance