从ViewModel MutableLiveData获取值并将其显示在文本字段上

时间:2019-09-30 18:25:11

标签: java android mvvm android-livedata android-viewmodel

我想了解Android架构库的知识,我一直在尝试通过在我的主活动上使用Mutable LiveData通过ViewModel显示来自改造的文本,但我似乎做不到,我非常感谢您的帮助。

这是我的模特班

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;
}

}

我的视图模型

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;
  }
}

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);
}

请协助

2 个答案:

答案 0 :(得分:0)

代替此

List<User> users =  response.body();

使用此

userDetails.postValue(response.body())

另外,在livedata之前删除静态

内部活动oncreate()初始化viewModel实例。 然后viewModel.getUserDEtails().obsever(this, data-> // your stuff);

答案 1 :(得分:0)

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

  }

然后在“活动”中观察更改

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.*/
                   }
                 }
            });