How to define a generic type of a data binding variable?
The following code never compiles.
<data> <variable name="viewModel" type="com.example.viewmodel.ViewModel<Model>"/> </data>
Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.
Data binding in Android (and many platforms as well) allow you to add objects in your XML layout and bind them together. It simplifies user interface management as changes in the underlying object is automatically reflected to views that are bound to it.
Android Studio Supports Data BindingSyntax Highlighting. XML code completion. Flagging of expression language syntax errors.
You need to escape <Model> as shown below:
<data> <variable name="viewModel" type="com.example.viewmodel.ViewModel<Model>"/> </data>
Android Studio will still show a "Cannot resolve symbol" error, but the XML will compile. It is a known issue. From Android Studio Support for Data Binding:
Note: Arrays and generic types, such as the Observable class, might display errors when there are no errors.
Try this one:
<data> <variable name="viewModel" type="com.example.viewmodel.ViewModel<Model>"/> </data>
<
is responsible for < and >
is responsible for >.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With