Skip to content Skip to sidebar Skip to footer

Can't Get Android Two-way Data Binding To Work (intellij Idea)

I am facing a problem where I can't get two-way data binding to work in IntelliJ IDEA. One-way binding works fine. Here is my setup: IntelliJ IDEA Ultimate 2016.2.1 Android API: 2

Solution 1:

For the attribute android:text databindings try to call some getText() on the EditText expecting a return type of String. But since it returns a CharSequence you have to define this getter yourself.

@InverseBindingAdapter(attribute = "android:text")
publicstaticStringcaptureTextValue(TextView view) {
    return view.getText().toString();
}

Solution 2:

I figured it out, based on the info from these slides:

http://www.slideshare.net/radekpiekarz/deep-dive-into-android-data-binding

  1. In the project's top-level build.gradle file, gradle:2.1.0 or above is needed. I had it set to gradle:2.0.0, even though I had the latest version installed. I am guessing this is the Android Gradle plugin version that @CommonsWare is referring to. Here is my corrected top-level build.gradle:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.1.0'
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
  2. After fixing the gradle version, IntelliJ IDEA still highlights the syntax as error, but the project compiles fine. However, the two-way data binding itself still did not work. To fix that part, I added notifyPropertyChanged(BR.name) to the name property setter in the Customer class:

    publicclassCustomerextendsBaseObservable {
        privateString name;
        private int age;
    
        @BindablepublicStringgetName() {
            return name;
        }
    
        publicvoidsetName(String name) {
            this.name = name;
            notifyPropertyChanged(BR.name);
        }
    
        @Bindablepublic int getAge() {
            return age;
        }
    
        publicvoidsetAge(int age) {
           this.age = age;
        }
    }
    

After this, the binding works as expected - I did not need to add any extra adapters or more code.

Post a Comment for "Can't Get Android Two-way Data Binding To Work (intellij Idea)"