Why does my activity doesn't see an observed object change?

后端 未结 3 696
萌比男神i
萌比男神i 2021-01-27 07:37

I\'m new to Android development and i am trying to understand Live Data with MVVM architecture. I am trying to make the main activity recognize when there is a change in an obje

相关标签:
3条回答
  • 2021-01-27 07:42

    Solution by OP.

    As Neha Rathore suggested this is the solution that works for me:

    in the view Model:

    public void login(String userName , String hashedPassword) {
        usersRepository.login(userName, hashedPassword, new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                loggedInUser.setValue(response.body());
            }
    
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loggedInUser.setValue(null);
            }
        });
    }
    

    and in the Repository:

    public void login(String username, String hashedPassword,@Nullable final Callback<String> callback){
        final MutableLiveData<String> loggedInUser = new MutableLiveData<>();
        User user = new User(username,hashedPassword);
    
        usersRepositoryApi.login(user).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    callback.onResponse(call,response);
                }
            }
    
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                callback.onFailure(call,t);
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-27 07:46

    here your problem is with repository code inside repository you are creating new object of mutable live data and observing different one.

    Interface Callback{
        onSuccess(String response)
        onError(String error)
    }
    
    public void login(String username , String hashedPassword,Callback callback){
        final MutableLiveData<String> loggedInUser = new MutableLiveData<>();
        User user = new User(username,hashedPassword);
    
        usersRepositoryApi.login(user).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    callback.onSuccess(response.body());
                }
            }
    
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                callback.onError(null);
            }
        });
    }
    
    //login method of your viewmodel 
    public void login(String userName , String hashedPassword) {
         usersRepository.login(userName, hashedPassword,new Callback(){
              void onSuccess(String responsebody){
                   loggedInUser.setValue(responsebody);
              }
    
              void onError(String error){
                   loggedInUser.setValue(responsebody);
              }
         });
    }
    
    0 讨论(0)
  • 2021-01-27 07:53

    In your repository, try changing this part:

    loggedInUser.setValue(response.body());
    

    to postValue function. like that:

    loggedInUser.postValue(response.body());
    
    0 讨论(0)
提交回复
热议问题