Can't Get Android Two-way Data Binding To Work (intellij Idea)
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
In the project's top-level
build.gradlefile, 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-levelbuild.gradle:buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.0' } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }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 thenameproperty 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)"