使用实时数据更新字符串值

时间:2018-06-04 12:15:10

标签: android data-binding android-architecture-components android-livedata android-jetpack

我正在使用实时数据架构实现数据绑定。我做了所有的事情,但我无法找到我必须更新片段中的响应值。 我做了一个按钮,然后点击按钮,我就可以将数据发送到服务器并获得响应。但我的问题是如何在片段中更新msg值或Success值。当值更新时,我的观察者没有调用。有什么我做错了实施。提前谢谢。

我的片段

  **Oncreate Method**
            {        signUpViewModel=ViewModelProviders.of(getActivity()).get(SignUpViewModel.class);
                observeViewModel(signUpViewModel);
              return view;
            }

//my observe class
            private void observeViewModel(SignUpViewModel signinViewModel) {
                    if (signinViewModel.getmessage() != null) {
                        signinViewModel.getmessage().observe(this, new Observer<String>() {
                            @Override
                            public void onChanged(@Nullable String msg) {
                                if (msg != null) {
                                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }

//button clicked     
            public void onSignUpClicked(View view)
            {   
     signUpViewModel.registerUser(userFName, userLName, userEmail,userPassword);
                    }

SignUpViewModel.class

public class SignUpViewModel extends ViewModel{
    private LoginFragmentRepository loginFragmentRepository;
    private LiveData<String> sucessMsg;
    String signUpMsg;

    public  void registerUser(String userFName, String userLName, String userEmail, String userPassword) {
       loginFragmentRepository=new LoginFragmentRepository();
        sucessMsg= loginFragmentRepository.registerUser( userFName,  userLName,  userEmail,  userPassword);
    }

    /**
     * get signup msg
     * @return
     */
    public LiveData<String> getmessage() {
        return sucessMsg;
    }

LoginFragmentRepository

{
    private ApiInterface apiInterface;
    private UserDao userDao;
public LiveData<String> registerUser(String userFName, String userLName, String userEmail, String userPassword) {
        apiInterface = RestApi.getRetroInstance().create(ApiInterface.class);
         final MutableLiveData<String> msg = new MutableLiveData<>();
        Call<SignUpModel> req = apiInterface.registerUser(userFName,userLName,userEmail,userPassword);
        req.enqueue(new Callback<SignUpModel>() {
            @Override
            public void onResponse(Call<SignUpModel> call, Response<SignUpModel> response) {
                if (response.isSuccessful()) {
                  //update this value in fragment
                    msg.postValue(response.body().getData());
                    //getReceipesListDataResponseMutableLiveData.setValue(NetworkResponse.success(receipes));
                } else {
                    msg.postValue("Please check your network connection");
                }
            }

            @Override
            public void onFailure(Call<SignUpModel> call, Throwable t) {
                msg.postValue("Please check your network connection");
            }
        });
        return msg;
    }
}

1 个答案:

答案 0 :(得分:1)

您的onCreateView方法被调用一次,以下条件为真,您永远不会注册一个observable:

if (signinViewModel.getmessage() != null) {
                        signinViewModel.getmessage().observe(this, new Observer<String>() {
                            @Override
                            public void onChanged(@Nullable String msg) {
                                if (msg != null) {
                                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

我建议你做的就是这个。将视图模型中的registerUser方法更改为:

 public  LiveData<String> registerUser(String userFName, String userLName, String userEmail, String userPassword) {
       loginFragmentRepository=new LoginFragmentRepository();
       return loginFragmentRepository.registerUser( userFName,  userLName,  userEmail,  userPassword);
    }

然后在你的片段中,观察你单击按钮的时间。

public void onSignUpClicked(View view)
            {   
     signUpViewModel.registerUser(userFName, userLName, userEmail,userPassword).observe(this, new Observer<String>() {
                            @Override
                            public void onChanged(@Nullable String msg) {
                                if (msg != null) {
                                    Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }