简体   繁体   English

双向数据绑定在Android中不起作用

[英]Two-way data binding not working in Android

I know there have been some discussions about this in the past like this one . 我知道已经有在过去就好了一番讨论这一个 However, it doesn't seem to be working for me. 但是,它似乎不适用于我。 Here's what I have in my layout: 这是我的布局中的内容:

  <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{word.contents}"
        android:id="@+id/wordView"/>
    <EditText android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/wordInput"
        android:text="@={word.contents}"/>

I expect my TextView show the updated text in EditText as I type. 我希望我的TextView在键入时在EditText中显示更新的文本。 But it doesn't happen. 但这不会发生。 I can make it work by implementing my own Text Watchers but I thought the whole point of data-binding was to avoid doing that. 我可以通过实现自己的Text Watchers使其工作,但我认为数据绑定的全部目的是避免这样做。 Am I missing anything here? 我在这里想念什么吗? Do I need to be doing more? 我需要做更多的事情吗? I found some people suggesting to make the data obeservable or bindable but I'd rather not mess up my model objects. 我发现有人建议使数据可观察或可绑定,但我不想弄乱我的模型对象。 Here's my gradle dependency: 这是我的gradle依赖项:

classpath 'com.android.tools.build:gradle:2.2.3'

and my app gradle has: 和我的应用程序gradle有:

dataBinding {
    enabled = true
}

and in my MainActivity's onCreate: 在我的MainActivity的onCreate中:

MainBinding binding = DataBindingUtil.setContentView(this, R.layout.main);
Word word = new Word("Test Word");
binding.setWord(word);

and here's the Word class, a simple POJO: 这是Word类,一个简单的POJO:

public class Word {
    private String contents;

    public Word() {
        contents = "";
    }

    public Word(String contents) {
        this.contents = contents;
    }

    public String getContents() {
        return contents;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }
}

Also, I debugged the app leaving a breakpoint in my setContents method. 另外,我调试了该应用程序,在setContents方法中留下了断点。 I can see upon changing EditText, code stops at the breakpoint and model actually changes. 我可以看到在更改EditText时,代码在断点处停止,并且模型实际上发生了变化。 It just looks like the TextView component doesn't get updated. 看起来TextView组件没有更新。 Any idea? 任何想法?

you just need to use BaseObservable in your model class ie Word and notify that property is changed. 您只需要在模型类(即Word使用BaseObservable ,并通知该属性已更改。

public class Word extends BaseObservable {
    public void setContents(String contents) {
        this.contents = contents;
        notifyPropertyChanged(BR.contents);
    }
}

Update your Java file like, 像这样更新您的Java文件,

MainBinding binding = DataBindingUtil.setContentView(this, R.layout.main);
Word word = new Word();
word.setContents("Test Word");
binding.setWord(word);

Can you update Word class like , 您可以更新Word类吗,

public class Word extends BaseObservable {
private String contents;

public Word() {
    contents = "";
}

public Word(String contents) {
    this.contents = contents;
}

@Bindable
public String getContents() {
    return contents;
}

public void setContents(String contents) {
    this.contents = contents;
    notifyPropertyChanged(BR.contents);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM