我正在学习android mvvm结构,我在实现它时有一个疑问。我正在使用房间在数据库中插入一个用户对象。但是,我不知道插入成功后如何通知活动。
我试图实现它。但是,它没有用。让我展示我的代码。
注册活动
public class RegisterActivity extends AppCompatActivity {
ActivityRegisterBinding activityRegisterBinding;
RegisterViewModel registerViewModel;
private final CompositeDisposable mDisposable = new CompositeDisposable();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDataBinding();
onUserRegistered();
}
private void initDataBinding() {
activityRegisterBinding = DataBindingUtil.setContentView(this, R.layout.activity_register);
RegisterViewModelFactory registerViewModelFactory = Injection.provideRegisterViewModelFactory(this);
registerViewModel = ViewModelProviders.of(this,registerViewModelFactory).get(RegisterViewModel.class);
activityRegisterBinding.setRegisterViewModel(registerViewModel);
}
private void onUserRegistered() {
findViewById(R.id.register_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDisposable.add(registerViewModel.onClickedSignUp()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action() {
@Override
public void run() throws Exception {
Log.e("Success!!!","added a record");
Toast.makeText(RegisterActivity.this,"success",
Toast.LENGTH_SHORT).show();
onBackPressed();
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("Unable to add a record", throwable.toString());
throwable.printStackTrace();
}
}));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mDisposable.dispose();
}
}
注册视图模型
public class RegisterViewModel extends ViewModel {
public ObservableField<String> name;
public ObservableField<String> password;
private UserDataSource userDataSource;
RegisterViewModel(UserDataSource userDataSource){
name=new ObservableField<>("");
password=new ObservableField<>("");
this.userDataSource=userDataSource;
}
public TextWatcher nameWatcher=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(!name.get().equals(s.toString())){
name.set(s.toString());
}
}
};
public TextWatcher passwordWatcher=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(!password.get().equals(s.toString())){
password.set(s.toString());
}
}
};
public Completable onClickedSignUp() {
isLoading.setValue(true);
if (name != null)
Log.e("name", name.get());
if (password != null)
Log.e("pass", String.valueOf(password.get()));
return Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
Log.e("final name",name.get());
User user=new User(0,name.get(),0,0);
userDataSource.insertOrUpdateUser(user);
}
});
}
}
我在从视图模型打印的日志中获得了正确的名称和密码值,但是该记录未在按钮上的单击事件上添加,也不会通知活动成功或失败。 在这种情况下该怎么办?
请指导我,因为我是 mvvm 的新手并学习它。
数据访问对象接口中插入方法的返回无效。插入数据时,您不会收到回调。您可以在调用插入方法后立即执行要执行的任务
另外,我建议您以活动的方法处理您在mDisposable 中添加onDestroy
一次性用品