Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data binding generic variable

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> 
like image 251
Bolein95 Avatar asked May 12 '16 06:05

Bolein95


People also ask

What is Data Binding with example?

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.

What is Data Binding in MVVM?

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.

Which of the following is included in Data Binding?

Android Studio Supports Data BindingSyntax Highlighting. XML code completion. Flagging of expression language syntax errors.


2 Answers

You need to escape <Model> as shown below:

<data>     <variable         name="viewModel"         type="com.example.viewmodel.ViewModel&lt;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.

like image 157
Mark Lu Avatar answered Sep 29 '22 05:09

Mark Lu


Try this one:

<data> <variable     name="viewModel"     type="com.example.viewmodel.ViewModel&lt;Model&gt;"/> </data> 

&lt; is responsible for < and &gt; is responsible for >.

like image 39
Hasan Abdullah Avatar answered Sep 29 '22 04:09

Hasan Abdullah