MutableLiveData的postValue方法返回null

时间:2019-08-02 16:46:37

标签: android mvvm android-livedata

我正在实现Mvvm模型,并尝试使用MutableLiveData侦听更改。 有两种方法

  1. setValue()-设置值。如果有活动的观察者,则会将值分派给他们。此方法必须从主线程调用。
  2. postValue()-需要从后台线程设置值时使用postValue。

这是我正在尝试的代码,使用postValue以及使用setValue时得到的都是null。

ViewModel

private MutableLiveData<List<Model>> modelsLiveData = new MutableLiveData<>();

public MainActivityViewModel() {
    modelsLiveData = new MutableLiveData<>();
}

public MutableLiveData<List<Model>> getModelsLiveData() {
    return modelsLiveData;
}

public void setModelsLiveData(final List<Model> modelsList) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Log.i("LIVE_DATA_TEST", Thread.currentThread().toString());
                Thread.sleep(1000);
                modelsLiveData.postValue(modelsList);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }).start();


}

MainActivity

 ActivityMainBinding activityBinding = DataBindingUtil
            .setContentView(this, R.layout.activity_main, new AppDataBindingComponent<>(this));
    MainActivityViewModel viewModel = new MainActivityViewModel();
    activityBinding.setViewModel(viewModel);

    Log.i("LIVE_DATA_TEST", "onCreate: "+ Thread.currentThread());

    List<Model> modelsList = new ArrayList<>();
    modelsList.add(new Model("Suzuki Swift", 2761111));
    modelsList.add(new Model("Renoult Megan", 3679060));
    modelsList.add(new Model("Fiat Panda", 4638764));
    modelsList.add(new Model("Fiat 500", 1234567));
    modelsList.add(new Model("Toyota Land Crouser", 9876543));

    viewModel.setModelsLiveData(modelsList);

布局

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable
        name="viewModel"
        type="com.matanmarciano.mvvmlivedata.viewmodels.MainActivityViewModel" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/models_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:items="@{viewModel.modelsLiveData}" />
</LinearLayout>

DataBindingAdapter

@BindingAdapter({"items"})
public void items(final RecyclerView recyclerView, List<Model> models) {
    Log.i("LIVE_DATA_TEST", "Let's see if this method is reached" + models);

    // TODO: update recyclerView adapter...
}

日志 这就是我得到的

I/LIVE_DATA_TEST: onCreate: Thread[main,5,main]
I/LIVE_DATA_TEST: Thread[Thread-4,5,main]
I/LIVE_DATA_TEST: Let's see if this method is reached null // null I am getting here when using postValue.

请帮助我哪里做错了。

1 个答案:

答案 0 :(得分:3)

您必须添加setLifecycleOwner才能从数据绑定中订阅liveData。

@Override
public int compareTo(Wonder other) {
    if(hostility == other.hostility) {
       // let's compare the strings list when hostility integer are equals (84, 84)
       String firstOtherCountry = other.countries.SortAlphabetically().get(0);           
       // we sort the countries list for current line and other wonder
       // you compare alphabetically first element of each list : 
       // return 1, 0 or -1 here.
    } else if(hostility < other.hostility) {
        return -1;
    } else if(hostility > other.hostility) {
        return 1;
    } else {
        return 0;
    }
}

https://developer.android.com/topic/libraries/data-binding/architecture#livedata