Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between import and variable when Data Binding?

After reviewing Data Binding documentation on the difference between <import/> and <variable/>, it's unclear how they differ. Below are examples taken from the documentation page.

<import type="com.example.real.estate.View" alias="Vista"/>

looks like the equivalent of

<variable name="user" type="com.example.User"/>

except that an alias can start with a capital letter, while name cannot. They are even used similarly.

<data>
    <import type="com.example.MyStringUtils"/>
    <variable name="user" type="com.example.User"/>
</data>
…
<TextView
   android:text="@{MyStringUtils.capitalize(user.lastName)}"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

The only difference I'm seeing, from the examples, are that you can call an import's methods, but not a variable's.

like image 808
Shalbert Avatar asked Sep 02 '25 03:09

Shalbert


1 Answers

You use variable if you want to pass some data to views. In your example, you have user variable which is of type User and you use it to set users name in TextView. You can call variables methods - user.lastName is equivalent to user.getLastName()

With import you only specify class that you want to use, you don't pass any data. In your example, the imported utility class is used only to capitalize users name, which the capitalize method receives as an argument.

like image 52
LaVepe Avatar answered Sep 04 '25 18:09

LaVepe