Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
779 views
in Technique[技术] by (71.8m points)

how to combine Architecture Components with data binding on android?

I have developed app base on android data binding library: https://developer.android.com/topic/libraries/data-binding/index.html

class SignInViewModel extends BaseObservable {

    @Bindable
    public String getLogin() {
        return login;
    }

    @Bindable
    public String getPassword() {
        return password;
    }
}

and now I want to use ViewModelProviders from new library: https://developer.android.com/topic/libraries/architecture/guide.html

SignInViewModel signInViewModel = ViewModelProviders.of(this).get(SignInViewModel.class);

How it combine? any idea? or should be combined these two libraries?

Edit

I change to:

class SignInViewModel extends ViewModel {
   public ObservableField<String> login = new ObservableField<>("");

    public ObservableField<String> password = new ObservableField<>("");
}

and now compiles, but question is: is it right way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a known incompatibility. You can't extend BaseObservable and AndroidViewModel at the same time, so you can't use @Bindable making two-way data binding impossible*.

This will be fixed after arch components 1.0 final (on the data binding side).

*Edit: You can make your own ObservableViewModel: https://gist.github.com/JoseAlcerreca/4b66f9953d50b483d80e6b9ad7172685


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...