我已经在MVVM设计模式上实现了简单的手机号码登录,但是从服务器返回响应时未调用Onchanged函数。
LoginViewModel
public class LoginViewModel extends ViewModel {
private LoginModel login;
private MutableLiveData<LoginResponse> loginresponse = new MutableLiveData<>();
private LoginRepository loginrepository;
public void init() {
login = new LoginModel();
}
public LoginModel getLogin() {
return login;
}
public void onLoginClick() {
if(login.isPhonevalid()){
HashMap<String,Object> loginObject = new HashMap<>();
loginObject.put("phonenumber",login.getMobileNumber());
loginObject.put("apimethod","beatme");
loginresponse = LoginRepository.getInstance().login(loginObject);
// loginClick.setValue(login);
}
}
@BindingAdapter("error")
public static void setError(EditText editText, String errormessage) {
editText.setError(errormessage);
}
public MutableLiveData<LoginResponse> getLoginresponse() {
return loginresponse;
}
public String getMobileNo(){
return login.getMobileNumber();
}
}
登录资料库
public class LoginRepository {
private static LoginRepository loginRepository;
private Api api;
public static LoginRepository getInstance() {
if (loginRepository == null) {
loginRepository = new LoginRepository();
}
return loginRepository;
}
public LoginRepository() {
api = RetrofitService.createService(Api.class);
}
public MutableLiveData<LoginResponse> login(HashMap<String, Object> body) {
final MutableLiveData<LoginResponse> loginResponse = new MutableLiveData<>();
api.login(body).enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if (response.isSuccessful()) {
loginResponse.setValue(response.body());
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
loginResponse.setValue(null);
}
});
return loginResponse;
}
}
登录活动
public class LoginActivity extends AppCompatActivity {
private LoginViewModel viewModel;
ActivityLoginBinding activityLoginBinding;
boolean doubleBackToExitPressedOnce = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().hide();
activityLoginBinding = DataBindingUtil.setContentView(LoginActivity.this, R.layout.activity_login);
viewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
if (savedInstanceState == null) {
viewModel.init();
}
activityLoginBinding.setLoginViewModel(viewModel);
setuploginclick();
}
public void setuploginclick() {
viewModel.getLoginresponse().observe(this, new Observer<LoginResponse>() {
@Override
public void onChanged(LoginResponse loginResponse) {
Toast.makeText(LoginActivity.this,viewModel.getMobileNo(),Toast.LENGTH_SHORT).show();
Intent i = new Intent(LoginActivity.this, OTPVerify.class);
i.putExtra("mobileno",viewModel.getMobileNo());
startActivity(i);
}
});
}
}
从日志中可以看到数据已成功发布,并且响应也很完美。但是,将值分配给实时数据“ loginresponse”不会调用可观察的。
最佳答案
从loginResponse
删除ViewModel
并在Repository
中创建它,然后将存储库loginResponse
传递给您的活动。检查下面
public class LoginRepository {
public MutableLiveData<LoginResponse> loginResponse = new MutableLiveData<>();
...
//getter
public MutableLiveData<LoginResponse> getLoginResponse() {
return loginResponse
}
}
然后在ViewModel中:
public class LoginViewModel extends ViewModel {
...
public MutableLiveData<LoginResponse> getLoginResponse() {
return LoginRepository.getInstance().getLoginResponse()
}
...
public void onLoginClick() {
if(login.isPhonevalid()){
HashMap<String,Object> loginObject = new HashMap<>();
loginObject.put("phonenumber",login.getMobileNumber());
loginObject.put("apimethod","beatme");
LoginRepository.getInstance().login(loginObject);
// loginClick.setValue(login);
}
}
...
}
如下更改存储库中的登录功能:
public void login(HashMap<String, Object> body) {
api.login(body).enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if (response.isSuccessful()) {
loginResponse.postValue(response.body());
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
loginResponse.postValue(null);
}
}