无法让Android双向数据绑定工作(IntelliJ IDEA) [英] Can't get Android two-way data binding to work (IntelliJ IDEA)
问题描述
我正面临一个问题,我无法在IntelliJ IDEA中获取双向数据绑定。单向绑定工作正常。
这是我的设置:
IntelliJ IDEA Ultimate 2016.2.1
Android API:24
Java:1.8.0_102
Gradle:2.14.1
这是我的 build.gradle
文件:
应用插件:'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion24.0.1
defaultConfig {
applicationIdcom.example.xyz
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName1.0
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro '
}
debug {
debuggable true
}
}
dataBinding {
enabled = true
}
}
依赖关系{
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'
}
这是布局.xml(单向绑定):
<?xml version =1.0encoding =utf-8? >
< layout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/tools> ;
< data>
< variable name =customertype =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)}
工具: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:找到数据绑定错误。
**** /数据绑定错误**** msg:在android.widget.EditText上找不到属性android:text的值类型为java.lang.String的getter。
文件:C:\Source\Android\test\app\src\main\res\layout\test_layout.xml
loc:24:33 - 24:43
**** \数据绑定错误****
IntelliJ中不支持双向数据绑定但是,我还是缺少什么?
根据这些幻灯片的信息,我想出来了:
http://www.slideshare.net/radekpiekarz/deep-dive-into-android-data-binding
-
在项目的顶级
build.gradle
文件中,毕业时需要2.1.0或以上。我已经设置为毕业:2.0.0,即使我安装了最新版本。我猜这是@CommonsWare所指的Android Gradle插件版本。这是我更正的顶级build.gradle
:buildscript {
repositories {
jcenter()
}
依赖关系{
classpath'com.android.tools.build:gradle:2.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type:删除){
删除rootProject.buildDir
}
-
gradle版本,IntelliJ IDEA仍然突出显示语法错误,但项目编译正常。但是,双向数据绑定本身仍然没有起作用。要修复该部分,我将Customer $中的
notifyPropertyChanged(BR.name)
添加到Customer类中的名称
属性setter中: / p>
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;
}
}
此后,绑定按预期工作 - 我不需要添加任何额外的适配器或更多的代码。
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: 24
Java: 1.8.0_102
Gradle: 2.14.1
Here is my build.gradle
file:
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'
}
Here is the layout.xml (one-way binding):
<?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>
Here is the Customer class:
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;
}}
This works just fine for one-way data binding, however when I try two way data binding by changing this line to the following:
android:text="@={customer.name}"
First, the syntax is not recognized (turns red in IntelliJ) with error: "Missing /"....and the compiler error is:
Error:Gradle: Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the getter for attribute 'android:text' with value type java.lang.String on android.widget.EditText. file:C:\Source\Android\test\app\src\main\res\layout\test_layout.xml loc:24:33 - 24:43 ****\ data binding error ****
Is two-way data binding not supported in IntelliJ yet, or what am I missing?
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.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-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 thename
property setter in the Customer class: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; } }
After this, the binding works as expected - I did not need to add any extra adapters or more code.
这篇关于无法让Android双向数据绑定工作(IntelliJ IDEA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!