本文介绍了在MVVM中使用LiveData进行观察时,如何在onChanged()上制作单独的代码? [未解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我实现了

public class DriverLoginActivity extends BaseActivity implements Observer<LoginUser>

而且,onChanged太过夸张了.

And, onChanged overided.

 @Override
    public void onChanged(@Nullable LoginUser loginUser) {
        if (TextUtils.isEmpty(Objects.requireNonNull(loginUser).getEmail())) {
            binding.email.setError("Email Id can't be blank");
            binding.email.requestFocus();
        } else if (!loginUser.isEmailValid()) {
            binding.email.setError("Please enter valid Email Id");
            binding.email.requestFocus();
        } else if (TextUtils.isEmpty(Objects.requireNonNull(loginUser).getPassword())) {
            binding.password.setError("Password can't be blank");
            binding.password.requestFocus();
        } else if (!loginUser.isPasswordLengthGreaterThan5()) {
            binding.password.setError("Password length must be at least 8 digit");
            binding.password.requestFocus();
        } else {
            mAuth.createUserWithEmailAndPassword(loginUser.getEmail(), loginUser.getPassword()).addOnCompleteListener(this);
        }
    }

现在,对于此活动,我有2个按钮.因此,取决于按钮的单击,将调用此onChanged.现在,我想为登录用户和注册进行注册按钮.如何在onChanged方法中将其分开?

Now, for this activity, I have 2 buttons. so, the depending on the button click, this onChanged will be call. Now I want to do login button for login user and registre for registration. How can I make it separate in onChanged method?

请向我建议最好的做法.

Please suggest me the very best practice.

我不认为我应该在viewmodel类中使用setter并在活动中使用getter.它不考虑MVVM实践.对吧?

I don't think I should use setter in viewmodel class and getter in activity. It don't consider MVVM practice. right?

推荐答案

在MVVM中,ViewModel用于管理数据.理想的方法是在ViewModel中处理您的API调用.为此使用LiveData并不是LiveData的理想使用.

In MVVM, the ViewModel is designed to manage data. The ideal way would be handling your API calls in the ViewModel. Using LiveData for this is not the ideal use of LiveData.

您的ViewModel可以具有

Your ViewModel can have

public void onLoginClick(View view) {
    //Implement Login API call here
}


public void onRegisterClick(View view){
    //Implement Register API call here

}

在您的activity_main.xml中

And in your activity_main.xml

<Button
            android:id="@+id/btnLogin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="32dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:text="Login"
            android:onClick="@{(v) -> LoginViewModel.onLoginClick()}"

<Button
            android:id="@+id/btnRegister"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="32dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:text="Register"
            android:onClick="@{(v) -> LoginViewModel.onRegisterClick()}"

检查此 https://github.com/MindorksOpenSource/android-mvvm-architecture/tree/master/app/src/main/java/com/mindorks/framework/mvvm/ui/login

这篇关于在MVVM中使用LiveData进行观察时,如何在onChanged()上制作单独的代码? [未解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 09:29