1

我正在学习 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 的新手并正在学习它。

4

1 回答 1

0

数据访问对象接口中插入方法的返回是无效的。插入数据时,您不会收到回调。您可以在调用 insert 方法后执行您想要执行的任务

onDestroy另外,我建议您处理在活动方法中添加到 mDisposable 中的一次性用品

于 2018-04-10T06:49:31.980 回答