passing ArrayList as argument in safeArgs
S

1

15

I'm trying to pass an ArrayList<String> as an argument while using safeArgs. But when I select ArrayList from the list of serializable and build, it is throwing me these exceptions

One type argument expected for class ArrayList<E : Any!>
One type argument expected. Use 'ArrayList<*>' if you don't want to pass type arguments

I get that I have to set the dataType for the ArrayList but I'm not able to do it nor do I have options while setting up the safeArgs from the Navigation graph screen.

In the Navigation graph XML, the code is generated as

<argument
            android:name="teamsToAdd"
            app:argType="java.util.ArrayList" />

is there a way to declare the dataType in the XML of the navigation graph?

Sofko answered 2/6, 2020 at 4:46 Comment(0)
H
36

Try this:

<argument
    android:name="teamsToAdd"
    app:argType="string[]" />

You can also generate this by checking the Array option when generating an argument through the GUI:

enter image description here

As this will require a string array rather than a list, you can then convert your list to an array with:

parameterList.toTypedArray()

And back from an array with:

receivedArray.toList()
Hindemith answered 2/6, 2020 at 6:59 Comment(1)
nice workaround. Thanks a lot, but still I'd like to know if there's a way to use the serializable provided by android studio. I saw lot of examples where one can use custom parcelables (by just mentioning the path of the data class in the place of argType in the nav graph XML) but serializable would come in handy. Wish google documented these minute detailsSofko

© 2022 - 2024 — McMap. All rights reserved.