我如何才能使实时数据观察器基于用户单击事件并接受一些用户输入而工作,但是应该从onCreate开始观察? [英] How can I make it live data observer working based on user click event with take some user input, but should start observing from onCreate?

查看:72
本文介绍了我如何才能使实时数据观察器基于用户单击事件并接受一些用户输入而工作,但是应该从onCreate开始观察?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了改造,然后是存储库,然后是视图模型,然后是视图,但是在单击按钮观察器时不会在onChanged中观察到.来自onClick的OnClicking on按钮,一切正常.我在logcat中收到API响应,但是在onChanged中却没有调用它!

I have retrofit then repository then view model and then view, but while clicking on button observer don't observe in onChanged. OnClicking on button from onClick, everything is working. I'm getting API response in logcat, but in onChanged it is not called!

ManualLicenseKeyViewModel

ManualLicenseKeyViewModel

代码:

public class ManualLicenseKeyViewModel extends ViewModel {
    public MutableLiveData<String> key = new MutableLiveData<>();
    private MutableLiveData<License> mutableLiveData;
    private ManualLicenseRepository manualLicenseRepository;


    public void init() {
        if (mutableLiveData == null) {
            manualLicenseRepository = ManualLicenseRepository.getInstance();
        }
    }

    public LiveData<License> getLicenseRepository() {
        return mutableLiveData;
    }

    private void getLicenseData(String licenseKey, String macAddress, int productId) {
        mutableLiveData = manualLicenseRepository.getLicenseData(licenseKey, macAddress, productId);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                try {
                    getLicenseData(key.getValue(), "null", FIXED_PRODUCT_ID);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                break;


        }
    }

}

活动-onCreate:

activity - onCreate:

protected void init() {
        manualLicenseKeyViewModel.init();
        manualLicenseKeyViewModel.getLicenseRepository().observe(this, this);
    }


  @Override
    public void onChanged(License license) {
        showLog("Working?");
        try {
            showLog("license: " + license.getLicensekey());
        } catch (Exception e) {

        }
    }

注意:如果我通过init方法可以在以下情况下使用,但问题是,我想在用户点击事件中执行并从提交按钮之前的编辑文本中获取价值.另外,我也不想在视图模型本身中进行观察,因为我想要有关活动的数据!

NOTE: It is working in below case if I pass in init method, but the problem is, I want to perform in user click event and take value from edit text before submit button. Also I don't want to observe in view model itself, because I want data on activity!

 public void init() {
        if (mutableLiveData == null) {
            manualLicenseRepository = ManualLicenseRepository.getInstance();
            mutableLiveData = manualLicenseRepository.getLicenseData("QQQQQ", "null", 4);
        }
    }

再次说明:

问题是,如果我在onCreate中编写了观察语句,它将不会基于用户的单击事件进行观察.因为我只想在用户在编辑文本中填写值并提交按钮时才调用存储库API,所以只有我可以获取必须传递到存储库中才能进行API调用并将该值传递给API的值.

The problem is if I write observe statement in onCreate, it won't observe based on user click event. Because I want to only call repository API when user fill value in edit text and submit button, then only I can get value which I have to pass in repository to make API call and pass that value to API.

推荐答案

致电时

mutableLiveData = manualLicenseRepository.getLicenseData(licenseKey, macAddress, productId);

您已经将onCreate中的活动观察者设置为mutableLiveData的实例.设置mutableLiveData的新实例后,所有观察者都将被取消订阅,因为先前的实例不再存在,因此您的观察者将不会收到任何结果.

you have already set active observer in onCreate to the instance of mutableLiveData. After you set new instance of mutableLiveData all the observers will be unsubscribed, since the previous instance does not exist anymore, so your observer will not receive any results.

将数据发送给观察者的更好方式是MutableLiveData.postValue(value).在这种情况下,您无需创建新的实时数据实例并取消订阅观察者,而只是向活动观察者发布值.

Better way of sending data to observers would be MutableLiveData.postValue(value). In this case instead of creating new live data instance and unsubscribe observers you are just posting a value to the active observers.

但是,如果您只需要在用户按下按钮后获取一次许可证数据,则最好致电房间暂停功能并获取纯数据显示

But if you need to get license data only once after user press button, you should better call Room suspend function and get plain data to display

P.S.最好在onViewCreated中添加观察者,而不是在onCreate中添加

P.S. it's better to add observers in onViewCreated not in onCreate

这篇关于我如何才能使实时数据观察器基于用户单击事件并接受一些用户输入而工作,但是应该从onCreate开始观察?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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