LiveData观察失败

时间:2019-03-04 21:27:58

标签: android mvvm android-livedata

我试图用MVVM(IM新手)启动应用程序,但是我遇到了一些问题。

我需要获取livedata值,创建User对象,然后在我的postRequest中发送该对象。

这是我的loginViewModel:

public class LoginViewModel extends AndroidViewModel {

private UserRepository userRepository;

private MutableLiveData<String> user;
private MutableLiveData<String> password;
private MutableLiveData<String> email;

private MutableLiveData<Resource<User>> userObservable;

@Inject
public LoginViewModel(@NonNull Application application){
    super(application);

    this.userRepository = new UserRepository();
}

public MutableLiveData<String> getUser() {
    if(user == null)
        user = new MutableLiveData<>();
    return user;
}

public MutableLiveData<String> getPassword() {
    if(password == null)
        password = new MutableLiveData<>();
    return password;
}

public MutableLiveData<String> getEmail() {
    if(email == null)
        email = new MutableLiveData<>();
    return email;
}

public void loginOnClickListener(){

    User user = new User(getUser().getValue(), getPassword().getValue(), getEmail().getValue());

    userRepository.saveUser(user, userObservable);
}
}

在我的片段中,我在观察模式下具有每个参考值:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    loginViewModel = ViewModelProviders.of(getActivity(), viewModelFactory).get(LoginViewModel.class);
    binding.setLoginViewModel(loginViewModel);

    observeViewModel();
}

private void observeViewModel(){

    loginViewModel.getUser().observe(this, user -> {

        String coco = user;

    });

    loginViewModel.getPassword().observe(this, user -> {

        String coco = user;

    });

    loginViewModel.getEmail().observe(this, user -> {

        String coco = user;

    });
}

我在xml文件中有引用:

        <android.support.design.widget.TextInputLayout
        android:id="@+id/userLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="96dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/txtUserValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/user"
            android:text="@{loginViewModel.user}"
            android:inputType="text"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
    android:id="@+id/passwordLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/userLayout">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/txtPasswordValue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/password"
        android:text="@{loginViewModel.password}"
        android:inputType="textPassword"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/emailLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordLayout">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/txtEmailValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/email"
            android:text="@{loginViewModel.email}"
            android:inputType="textWebEmailAddress"/>
    </android.support.design.widget.TextInputLayout>

为什么我的观察被称为神经?有什么帮助吗?谢谢!! o /

1 个答案:

答案 0 :(得分:1)

我发现了。就是为了这个:

替换此:

android:text="@{loginViewModel.email}"

与此:

android:text="@={loginViewModel.email}"