使用mvvm和liveDate登录

我的liveDate有问题

活动内容:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActivityUserManagementBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_user_management);
    binding.setLoginObject(new LoginRequest());

    loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
    binding.setLoginClickListener(loginViewModel);

    getLoginResponse();
}

private void getLoginResponse() {
    loginViewModel.getLoginViewModelListener().observe(this, new Observer<LoginResponse>() {
        @Override
        public void onChanged(LoginResponse loginResponse) {
            Log.e(TAG, "onChanged: " );
            if (loginResponse != null)
                Toast.makeText(UserManagementActivity.this, loginResponse.msg, Toast.LENGTH_SHORT).show();
        }
    });
}

仓库:

public class LoginRepository {

private LoginRequest loginRequest;
private MutableLiveData<LoginResponse> loginResponseLiveListener;

public LoginRepository(LoginRequest loginRequest) {
    this.loginRequest = loginRequest;
    getLoginData();
}

public LiveData<LoginResponse> getLoginResponseLiveListener() {
    if(loginResponseLiveListener == null)
        loginResponseLiveListener = new MutableLiveData<>();
    Log.e(TAG, "getLoginResponseLiveListener: " );
    return loginResponseLiveListener;
}

private void getLoginData() {
    RetrofitClient.INSTANCE.callEndPoint().login(loginRequest).enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            if(response.body() != null) {
                loginResponseLiveListener.setValue(response.body());
                Log.e(TAG, "onResponse: " + response.body().msg);
            }

        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            Log.e(TAG, "onFailure: "+ t.getMessage() );
        }
    });
}

}

ViewModel:

public class LoginViewModel extends ViewModel {

private boolean isValid = true;
private LoginRepository loginRepository;
private MutableLiveData<LoginResponse> loginViewModelListener;

public void onSubmitLoginClicked(View submit, CustomEditText email, CustomEditText password, LoginRequest loginRequest) {
    if (loginValidation(email, password, loginRequest)) {
        loginRequest.setToken(MyTools.getKey(MyApplication.getApplication(), "token"));
        loginRepository = new LoginRepository(loginRequest);

        loginViewModelListener.setValue(loginRepository.getLoginResponseLiveListener().getValue());
    }
}

public MutableLiveData<LoginResponse> getLoginViewModelListener(){
    return loginViewModelListener;
}

private boolean loginValidation(CustomEditText email, CustomEditText password, LoginRequest loginRequest) {
    if (UserValidation.isNullOrEmpty(loginRequest.email) ||!UserValidation.isEmail(loginRequest.email)) {
        email.setError(MyApplication.getApplication().getString(R.string.error_login_email));
        isValid = false;
    }

    if (UserValidation.isNullOrEmpty(loginRequest.password) ) {
        password.setError(MyApplication.getApplication().getString(R.string.error_login_password));
        isValid = false;
    }

    return isValid;
}

}

我没有从活动中得到任何东西,因为返回的livedata对象为null! 那么问题出在哪里呢? 这是干净的代码吗?我将xml中的视图传递给视图模型,以使所有逻辑都基于视图模型。 请我需要您的帮助和您的建议。

评论
不要这样
不要这样

创建一个新的全局变量

private ActivityUserManagementBinding binding;

比从中删除

ActivityUserManagementBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_user_management);
binding.setLoginObject(new LoginRequest());

from your onCreate and put this inside onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle   savedInstanceState) {
    binding = DataBindingUtil.setContentView(this, R.layout.activity_user_management);  
    return bindings.root;
}
点赞
评论