I'm implementing data binding for my Android app using ObservableArrayMap
in my layout xml.
The map has a clearly defined types for its keys and values (e.g. ObservableArrayMap<String, Integer>
, BUT I keep getting the error error: incompatible types: Object cannot be converted to Integer
.
The cause of the error is obvious: the generated ViewBinding Java file completely ignores the types of the type arguments of the map and declares the map as android.databinding.ObservableArrayMap
. So, when accessing its values, it returns Object
instead of Integer
.
Why the hell it does that? I get that the types are erased during runtime, but come on, it must work, otherwise, what's the point of defining the maps type arguments in the code?
What's even weirder, the compiler does perform type checking: if you were to pass the argument of a wrong type to the get()
method in the layout xml, you get an error.