LoginSignup
4
2

More than 5 years have passed since last update.

DataBindingの演算子

Last updated at Posted at 2018-04-24

Javaとほとんど同じ

算術演算子 + - / * %
文字列連結演算子 +
論理演算子 && ||
バイナリビット演算子 & | ^
単項演算子 + - ! ~
シフト演算子 >> >>> <<
比較演算子 == > < >= <=
instanceof
グルーピング ()
リテラル: 文字、文字列、数字、null
キャスト
メソッド呼び出し
フィールド アクセス
配列アクセス []
三項演算子 ?:+1: 
例
android:text="@{String.valueOf(index + 1)}"
android:visibility="@{age < 13 ? View.GONE : View.VISIBLE}"
android:transitionName='@{"image_" + id}'

null合体演算子

android:text="@{user.displayName ?? user.lastName}"

プロパティ参照

android:text="@{user.lastName}"

NullPointerExceptionの回避

例
@{user.name}がnullの場合、ぬるぽを回避してnullが入る。
@{user.age}などintの場合は0が入る。

xml内でviewなどにアクセスしたいときは

<data>
        <import type="android.view.View" />
        <variable
            name="employee"
            type="com.example.Employee"/>
</data>

とViewのインポートを忘れずに

xml内でorを使う場合

andorid:visivility="@{user.role == 1 || 2 ? View.VISIBLE : View.GONE}"

ではなく

andorid:visivility="@{user.role == 1 || user.role == 2 ? View.VISIBLE : View.GONE}"

とする

textなどにintをいれたいときは

android:text="@{Integer.toString(user.role)}"
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2