Search code examples
javaandroidmvvmandroid-livedataandroid-viewmodel

Get values From ViewModel MutableLiveData and display them on textfields


I am trying to get a hang of the Android Architecture Library and i have been trying to display texts from retrofit via a ViewModel using Mutable LiveData on my Main Activity but i cant seem to do it, i would really appreciate some assistance.

Here is my Model Class

public class User {


@SerializedName("name")
@Expose
private String name;
@SerializedName("email")
@Expose
private String email;
@SerializedName("phone")
@Expose
private String phone;

public User() {
}

public String getUserName() {
    return name;
}

public void setUserName(String name) {
    this.name = name;
}

public String getUserEmail() {
    return email;
}

public void setUserEmail(String email) {
    this.email = email;
}

public String getUserPhone() {
    return phone;
}

public void setUserPhone(String phone) {
    this.phone = phone;
}

}

My View model

public class UserViewModel extends AndroidViewModel {

  private NodeAuthService api;
  private SharedPreferences pref;

  private static MutableLiveData<List<User>> userDetails = new 
  MutableLiveData<>();


  private Call<List<User>> call;

  public UserViewModel(@NonNull Application application) {
    super(application);
    api = AuthRetrofitClient.getInstance().create(NodeAuthService.class);
  }
  private String email = pref.getString("email", "");

  public void loadUser(){
    call = api.getUser(email);
    call.enqueue(new Callback<List<User>>() {
        @Override
        public void onResponse(Call<List<User>> call, Response<List<User>> 
        response) {
            List<User> users =  response.body();
        }

        @Override
        public void onFailure(Call<List<User>> call, Throwable t) {
            Log.d("USER",t.getMessage());
        }
    });

  }

  public MutableLiveData<List<User>> getUserDetails(){
    return userDetails;
  }
}

Simplified version of my MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    TextView navName = (TextView) findViewById(R.id.navigation_name);
    TextView navEmail = (TextView) findViewById(R.id.navigation_email);
}

Kindly assist


Solution

  • In your viewModel

    public void loadUser(){
        call = api.getUser(email);
        call.enqueue(new Callback<List<User>>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> 
            response) {
                userDetails.postValue(response.body())
            }
    
            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {
                Log.d("USER",t.getMessage());
            }
        });
    
      }
    

    And in your Activity observe the changes

    yourVm.getUserDetails().observe(this, models -> {
                    if (models != null) {
                        for(int = 0; i<models.size();i++){
                        /*you will get your list here now iterate through list and get 
                        your email_id here.*/
                       }
                     }
                });