0

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.

Sevastyan Savanyuk
  • 5,797
  • 4
  • 22
  • 34
  • Could you add MVCE? just the part how you declare it inside xml and the part where you wana use it (and version of databinding lib)... dirty solution I think about is to use `MyClass extends ObservableArrayMap` – Selvin Dec 05 '17 at 10:57
  • @Selvin take a look at my detailed question here: https://stackoverflow.com/questions/47638336/data-binding-observablefield-with-lambda-value-doesnt-compile – Sevastyan Savanyuk Dec 05 '17 at 11:17

0 Answers0