Search code examples
androidandroid-jetpackandroid-navigationandroid-architecture-navigationandroid-jetpack-navigation

how to convert Array<(out) Event!> to and from ArrayList<Event> after passing data using safe args in Navigation Controller?


I am trying pass ArrayList data from one fragment to another fragment using safe args, here is the argument

<argument
        android:name="upcomingEvents"
        app:argType="com.x.Models.Event[]" />

but when I want to pass data using safe args, I have this error enter image description here

it is said that

type mismatch.

required: Array<(out) Event!>

Found: ArrayList

how to convert ArrayList<Event> to Array<(out) Event!> ?

and also vice versa , how to convert Array<(out) Event!> to ArrayList<Event> ?


Solution

  • Kotlin has built in methods for both operations, namely, toTypedArray() and asList():

    val upcomingEvents = currentUserData!!.upcomingEvents
    val array = upcomingEvents.toTypedArray()
    
    // On the other side
    val array = ...
    val list = array.asList()