Java 如何在ViewModel中观察LiveData?

Java 如何在ViewModel中观察LiveData?,java,android,android-livedata,android-viewmodel,Java,Android,Android Livedata,Android Viewmodel,在PostsbylableViewModel上,它有MutableLiveData令牌此令牌在每次滚动recyclerView时都会更改,我需要在PostsbylableViewModel中观察它,而不是从UI,因为我试图在recyclerView.addOnScrollListener中更改它,而应用程序被冻结和挂起。代码如下: public class PostsByLabelViewModel extends ViewModel { public static final Str

PostsbylableViewModel
上,它有
MutableLiveData令牌
此令牌在每次滚动recyclerView时都会更改,我需要在
PostsbylableViewModel
中观察它,而不是从UI,因为我试图在
recyclerView.addOnScrollListener
中更改它,而应用程序被冻结和挂起。代码如下:

public class PostsByLabelViewModel extends ViewModel {

    public static final String TAG = "PostsByLabelViewModel";

    public MutableLiveData<PostList> postListMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> finalURL = new MutableLiveData<>();
    public MutableLiveData<String> token = new MutableLiveData<>();

    public void getPostListByLabel() {

        Log.e(TAG, finalURL.getValue());

       PostsByLabelClient.getINSTANCE().getPostListByLabel(finalURL.getValue()).enqueue(new Callback<PostList>() {
            @Override
            public void onResponse(Call<PostList> call, Response<PostList> response) {
                PostList list = response.body();

                if (list.getItems() != null) {
                    Log.e(TAG, list.getNextPageToken());
                    token.setValue(list.getNextPageToken());
                    postListMutableLiveData.setValue(list);
                }
            }

            @Override
            public void onFailure(Call<PostList> call, Throwable t) {

            }
        });
    }
}

但我犯了运行时错误

error: incompatible types: PostsByLabelViewModel cannot be converted to LifecycleOwner
                token.observe(PostsByLabelViewModel.this, new Observer<String>() {
错误:不兼容的类型:PostSBylableViewModel无法转换为LifecycleOwner
token.observe(PostsByLabelViewModel.this,new observator()){
那么,我怎样才能在每一次更改时都在标记上进行观察呢

我需要在
PostsbylableViewModel
中观察它,而不是从UI

你可以用。只是当不再需要的时候别忘了打电话

…应用程序已冻结并挂起

您正在主线程上调用
PostsByLabelClient.getINSTANCE().getPostListByLabel(finalURL.getValue()).enqueue
。将其移动到后台线程

error: incompatible types: PostsByLabelViewModel cannot be converted to LifecycleOwner
                token.observe(PostsByLabelViewModel.this, new Observer<String>() {