无法让Android双向数据绑定工作(IntelliJ IDEA)

时间:2016-08-07 21:47:16

标签: android intellij-idea data-binding

我遇到的问题是我无法在IntelliJ IDEA中使用双向数据绑定。单向绑定工作正常。

这是我的设置:

  

IntelliJ IDEA Ultimate 2016.2.1

     

Android API:24

     

Java:1.8.0_102

     

Gradle:2.14.1

这是我的build.gradle文件:

apply plugin: 'com.android.application'
android {
   compileSdkVersion 24
   buildToolsVersion "24.0.1"

defaultConfig {
    applicationId "com.example.xyz"
    minSdkVersion 17
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        debuggable true
    }
}
dataBinding {
    enabled = true
}
}

dependencies {
   compile fileTree(include: ['*.jar'], dir: 'libs')
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:support-v4:24.1.1'
   compile 'com.android.support:design:24.1.1'
}

这是layout.xml(单向绑定):

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
<data>
    <variable name="customer"   type="com.example.xyz.model.Customer"/>
</data>
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp">
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@{Integer.toString(customer.age)}"
              tools:text="33"/>
    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{customer.name}"/>
</LinearLayout>

以下是Customer类:

public class Customer extends BaseObservable {
private String name;
private int age;

@Bindable
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Bindable
public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}}

这适用于单向数据绑定,但是当我通过将此行更改为以下内容来尝试双向数据绑定时:

android:text="@={customer.name}"

首先,语法无法识别(在IntelliJ中变为红色),并显示错误:“Missing /"....,编译错误为:

  

错误:Gradle:任务':app:compileDebugJavaWithJavac'的执行失败。   java.lang.RuntimeException:发现数据绑定错误。   **** /数据绑定错误****消息:找不到android.widget.EditText上值类型为java.lang.String的属性'android:text'的getter。   文件:C:\源\的Android \测试\应用\ SRC \主\ RES \布局\ test_layout.xml   当地时间:24:33 - 24:43   **** \数据绑定错误****

IntelliJ中是否还不支持双向数据绑定,或者我缺少什么?

2 个答案:

答案 0 :(得分:0)

对于属性android:text数据绑定,尝试在getText()上调用一些EditText,期望返回类型为String。但是因为它返回CharSequence,你必须自己定义这个getter。

@InverseBindingAdapter(attribute = "android:text")
public static String captureTextValue(TextView view) {
    return view.getText().toString();
}

答案 1 :(得分:0)

根据这些幻灯片中的信息,我想出来了:

  

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

  1. 在项目的顶级build.gradle文件中,需要gradle:2.1.0或更高版本。我把它设置为gradle:2.0.0,即使我安装了最新版本。我猜这是@CommonsWare所指的Android Gradle插件版本。这是我更正的顶级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. 修复gradle版本后,IntelliJ IDEA仍会将语法突出显示为错误,但项目编译正常。但是,双向数据绑定本身仍然无法正常工作。要修复该部分,我将notifyPropertyChanged(BR.name)添加到Customer类中的name属性设置器:

    public class Customer extends BaseObservable {
        private String name;
        private int age;
    
        @Bindable
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
            notifyPropertyChanged(BR.name);
        }
    
        @Bindable
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
           this.age = age;
        }
    }
    
  3. 在此之后,绑定按预期工作 - 我不需要添加任何额外的适配器或更多代码。

相关问题