从其他类更新视图模型 [英] Update viewmodel from a different class

查看:86
本文介绍了从其他类更新视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解ViewModel和LiveData.

I am trying to understand ViewModel and LiveData.

在MainActivity中,我正在观察LiveData 在MyTask中,我正在LiveData上设置数据,该数据应显示在活动中.

In MainActivity, I am observing LiveData In MyTask, I am setting data on the LiveData, that should be displayed in the activity.

问题是MyTask中设置的数据没有在UI上更新.

Problem is data set in MyTask is not getting updated on the UI.

MainActivity

public class MainActivity extends AppCompatActivity {

    private MyViewModel viewModel;
    private TextView tv2;

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

        tv2 = findViewById(R.id.textView2);

        viewModel = ViewModelProviders.of(this).get(MyViewModel.class);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.setNameData("Button clicked");
                new MyTask(getApplication()).execute();
            }
        });

        viewModel.getNameData().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                tv2.setText(s);
            }
        });

    }
}

ViewModel类

public class MyViewModel extends AndroidViewModel {

    private MutableLiveData<String> nameData = new MutableLiveData<>();

    public MutableLiveData<String> getNameData() {
        return nameData;
    }

    public void setNameData(String name) {
        nameData.postValue(name);
    }


    public MyViewModel(@NonNull Application application) {
        super(application);
    }
}

MyTask类

public class MyTask extends AsyncTask<Void, Void, Void> {

    private MyViewModel viewModel;

    public MyTask(Application application){
        viewModel = new MyViewModel(application);
    }

    @Override
    protected Void doInBackground(Void... voids) {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        viewModel.setNameData("Done task");

    }
}

推荐答案

根据

"而不是尝试通过库或扩展来解决此问题 体系结构组件,它应该作为设计问题来面对.我们 建议您将事件视为州的一部分."

"Instead of trying to solve this with libraries or extensions to the Architecture Components, it should be faced as a design problem. We recommend you treat your events as part of your state."

我们不应该共享livedata或viewmodel实例.

We should not share the livedata or viewmodel instance.

Activity -> Viewmodel -> Repository

活动应包含viewModel的实例.如果单击该按钮,则应将其通知给viewModel,该模型将启动任务.在viewmodel中获得响应后,更新实时数据.它将在活动中自动得到通知.

Activity should contain instance of a viewModel. If the button is clicked, it should be notified to the viewModel which shall start the task. After getting response in viewmodel, update the livedata. It will automatically get notified in the Activity.

这篇关于从其他类更新视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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