'Android - Custom NavType for List<Enum<*>>
I am trying to create a custom NavType for Enum with below code:
class EnumListType<T : Enum<T>> : NavType<List<T>>(true) {
@Suppress("TYPE_MISMATCH_WARNING")
override fun get(bundle: Bundle, key: String): List<T> {
val found = bundle.getStringArrayList(key)
return found?.map { enumValueOf(it) } ?: emptyList()
}
@Suppress("TYPE_MISMATCH_WARNING")
override fun parseValue(value: String): List<T> {
return value.substring(1, value.length - 1)
.split(',')
.map { enum -> java.lang.Enum.valueOf(T::class.java, enum) }
}
override fun put(bundle: Bundle, key: String, value: List<T>) {
bundle.putStringArrayList(key, values.map { it.name } as ArrayList<String>)
}
}
Both for get and parseValue, my code fails to compile with below error:
Cannot use 'T' as reified type parameter. Use a class instead.
Is it even possible to achieve a solution for this? If yes, what's wrong with the above code and how to fix it up?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
