Help us understand the problem. What is going on with this article?

DataBindingとObservableField使用時にハマったこと

More than 1 year has passed since last update.

結論

public な ObservableField が入ったViewModelクラスを作る際には、get"ObservableField名"のメソッドをクラス内に作ってはいけない

前提

  • Android Studio 2.3.1
  • com.android.tools.build:gradle:2.2.3

問題の起きるコード

ViewModel.java
public class ViewModel {
    public ObservableField<String> text = new ObservableField<>();
    public ObservableLong id = new ObservableLong(0);

    public String getText(){
        return this.text.get();
    }

    public void setText(String text){
        this.text.set(text);
    }

    public Long getId() {
        return this.id.get();
    }

    public void setId(long id) {
        this.id.set(id);
    }
}
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="viewModel"
            type="com.example.ViewModel" />
    </data>

    <FlameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.example.CustomView
            android:id="@+id/custom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:Text="@{viewModel.text}"
            app:Id="@{viewModel.id}" />
    </FlameLayout>
</layout>
BindingAdapter.java
public class BindingAdapters {
    @BindingAdapter({"Text"})
    public static void setCustomViewText(CustomView customView, String text) {
        customView.setText(text);
    }

    @BindingAdapter({"Id"})
    public static void setCustomViewId(CustomView customView, long id) {
        customView.setId(id);
    }
}

起きること

  • 上記のViewModelインスタンスのsetIdメソッドを呼んでもsetCustomViewIdメソッドが呼ばれない

解決法

  • ViewModel.javaからgetTextとgetIdメソッドを削除する

原因

  • ObservableField hogeの値をgetするメソッドの名前をgetHogeにするとなぜかActivityHugaBindingクラスにonChangeHogeViewModelメソッドが作られないなど、自動コード生成の一部に不正が発生し、ObservableFieldの値が更新されてもそれを検知できない
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away