单击按钮时,Android ViewModel LiveData更新视图 [英] Android ViewModel LiveData update view on button click

查看:167
本文介绍了单击按钮时,Android ViewModel LiveData更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循教程来学习ViewModel和LiveData.就我而言,不是从网络获取数据,而是在按钮单击时生成随机字符串,并尝试更新textview.问题在于,通过单击按钮更改数据时,textview不会更新,而仅在切换方向时才更新.

I am following this tutorial to learn ViewModel and LiveData. In my case, instead of getting data from network, I am simply generating random string on button click and trying to update a textview. The problem is that the textview does not get updated when the data is changed by button click, but only gets updated when orientation is toggled.

活动类(扩展了LifecycleActivity)

public class PScreen extends BaseActivity {
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.places_screen);

final UserModel viewModel = ViewModelProviders.of(this).get(UserModel.class);
viewModel.init();

viewModel.getUser().observe(this, new Observer<User>() {
  @Override public void onChanged(@Nullable User user) {
    ((TextView) findViewById(R.id.name)).setText(user.getName());
  }
});

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View v) {
    final MutableLiveData<User> data = new MutableLiveData<>();
    User user = new User();
    user.setName(String.valueOf(Math.random() * 1000));
    data.postValue(user);
    viewModel.setUser(data); // Why it does not call observe()
  }
});
  }
}

ViewModel类

package timsina.prabin.tripoptimizer.model;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModel;

public class UserModel extends ViewModel {
  private LiveData<User> user;

  public void init() {
    if (this.getUser() != null) {
      return;
    }
    this.user = new LiveData<User>() {
      @Override protected void setValue(User value) {
        value.setName("Fresh New Name");
        super.setValue(value);
      }
    };
  }

  public LiveData<User> getUser() {
    return user;
  }

  public void setUser(LiveData<User> user) {
    this.user = user;
  }
}

推荐答案

您每次都在创建一个新的LiveData实例!你不应该那样做.如果这样做,所有以前的观察者都将被忽略.

You are creating a new LiveData instance each time! You are not supposed to do that. If you do that all previous observers will be ignored.

在这种情况下,您可以将ViewModel上的setUSer(LiveData<User>)方法替换为setUser(User u)(使用用户而不是LiveData),然后在其中执行user.setValue(u).

In this case you could replace your setUSer(LiveData<User>) method on your ViewModel to setUser(User u) (taking a User instead of a LiveData) and then do user.setValue(u) inside it.

当然,必须初始化ViewModel类中的LiveData成员,如下所示:

Of course, will have to initialize the LiveData member in your ViewModel class, like this:

final private LiveData<User> user = new MutableLiveData<>();

然后它将起作用,因为它将通知现有的观察者.

It will work then because it will notify the existing observers.

这篇关于单击按钮时,Android ViewModel LiveData更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆