How can I observe API call based on user submit button and at same time getValue from edit text which is necessary for submit action?

前端 未结 3 1661
灰色年华
灰色年华 2020-12-22 06:42

The problem is if I\'ll 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

相关标签:
3条回答
  • 2020-12-22 06:54

    I assume the methods you posted are from your Activity. Merely adding a method named onClick doesn't do anything at all; you need to attach a click listener to your button. For example, something like this in your onCreate method:

    findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                getLicenseData(key.getValue(), "null", FIXED_PRODUCT_ID);
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-22 07:06

    You have LiveData defined inside the method which is wrong. LiveData is something that's constantly observed by View(Activity) and therefore it needs to be held in ViewModel/Repository in a member variable.

    Also, your repository looks redundant (does not do anything). I suggest redefining LiveData as a member variable of your ViewModel, making the request inside the ViewModel and posting the value to LiveData.

    If you just need to send request and receive result then you don't need to even use LiveData, just use some Listener/Callback interface instead.

    0 讨论(0)
  • 2020-12-22 07:12

    First I will suggest you to Add data binding in your App

    now in your Activity XML do something like this

    <!--here it is 2 way binding-->
    <EditText
        ...
        android:text="@={viewmodel.key}
        ... />
    
    <!--here it is single way binding-->
    <Button
        ...
        android:onClick="@{() -> viewmodel.doneClicked()}"
        ... />
    

    Now in your ViewModel

    //value in key will be same as value in the EditText as it is bidirectional data binding
    public MutableLiveData<String> key = new MutableLiveData<>();
    
    //update this MutableLiveData accordingly
    private MutableLiveData<Boolean> isDataAvailable = new MutableLiveData<>();
    
    //observe this LiveData
    LiveData<Boolean> isDataAvailable(){
        return isDataAvailable;
    }
    
    public void doneClicked(){
        //here you can get value of your "key"
        //this method will call when clicking on the button
        //you can call your custom method from here,
        //also update data in "isDataAvailable"
    }
    
    

    In your Activity

    viewModel.isDataAvailable().observe(this, aBoolean -> {
        //here you will receive change in value of isDataAvailable
    }
    

    PS Your question is not clear at all, and your code is not helpful

    Do you want to observe change in mutableLiveData of getLicenseData(...) inside your viewModel?

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

    Please also change the name of this variable to something related the data it is getting, mutableLiveData is not a good variable name

    Edit 1 try this to observe changes in the data, it is recommended to observe LiveData but you can observer MutableLiveData as you have not provided sufficient information I m using your mutableLiveData variable

    In your Activity try this

    viewModel.mutableLiveData.observe(this, license -> {
        //here you can observe changes in your mutableLiveData when It receives data from Repo
        //here license is the object of data inserted into mutableLiveData from repo
    });
    

    Please explain more.

    Hope this will help!

    0 讨论(0)
提交回复
热议问题