Variable value is not updated when input field changes

  android, java, kotlin

I can’t understand why the value of a variable firstName is not updated in MVVM.
variable in class Model

@get:Bindable
var firstName: String = _firstName
    set(value) {
        _firstName = value
        field = value
        notifyPropertyChanged(BR.firstName)
    }

Code in ViewModel

private val _firstName = MutableLiveData<String>()
val firstName: LiveData<String>
    get() = _firstName


fun getFirstName() {
    _firstName.value = model.firstName
}

code in fragment class

binding.lifecycleOwner = viewLifecycleOwner
        binding.registerModel = registerViewModel
        registerViewModel.firstName.observe(viewLifecycleOwner,androidx.lifecycle.Observer {
            registerViewModel.getFirstName()
        })

getting a value in a fragment

android:text="@{registerModel.model.firstName}"

Source: Android Questions

LEAVE A COMMENT