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

Update String Value Using Live Data

我正在使用实时数据架构实现数据绑定。我做了所有的事情,但我无法找到我必须在哪里更新片段中的响应值。 我制作了一个按钮并单击按钮我能够将数据发送到服务器并获得响应。但我的问题是如何在片段中更新 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;
    }
}

您的 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();
                                }
                            }
                        });
                    }