Skip to content Skip to sidebar Skip to footer

Android Livedata Not Updating Edittext

I am using Android LiveData in 3 different EditText. I have to show the result of multiplying the values of the first two EditText into the third EditText. I took advantage of an a

Solution 1:

Something similar to this. To avoid cyclic updates you may just compare new value inside onFirstChanged/onSecondChanged with value in your liveData and skip liveData.value = newValue in that way.

class MainActivity : AppCompatActivity() {
    private lateinit var binding: MainActivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        binding = DataBindingUtil.setContentView(
            this,
            R.layout.main_activity
        )

        viewModel =
            ViewModelProvider(this, factory)[MainActivityViewModel::class.java]

        initView(binding)
    }

    private fun initView(
        binding:
        MainActivityBinding
    ) {
        binding.num1.listenChanges { viewModel.onFirstChanged(it) }
        binding.num2.listenChanges { viewModel.onSecondChanged(it) }

        viewModel.num1
            .observe(
                lifecycleOwner,
                Observer { num1Value ->
                    binding.num1.setText(num1Value.toString())
                }
            )
        viewModel.num2
            .observe(
                lifecycleOwner,
                Observer { num2Value ->
                    binding.num2.setText(num2Value.toString())
                }
            )
        viewModel.num3
            .observe(
                lifecycleOwner,
                Observer { result ->
                    binding.num3.setText(result.toString())
                }
            )
    }

    binding.num1.isFocusableInTouchMode = true
    binding.num2.isFocusableInTouchMode = true
    binding.num3.isFocusableInTouchMode = true

}

private fun EditText.listenChanges(textChanged: (String) -> Unit) {
    addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            textChanged(s.toString())
        }
    })
}

class RapportiAltriCostiViewModel(private val repositoryDB: DbRepository) : ViewModel() {

    val num1 = MutableLiveData<Int>(0)
    val num2 = MutableLiveData<Double>(0.0)
    val num3: LiveData<Double>
        get() = num1.combineWith(num2) { first, second ->
            (first ?: 0) * (second ?: 0.0)
        }

    fun onFirstChanged(newValue: Int) {
        if (num1.value != newValue) {
            num1.value = newValue
        }
    }

    fun onSecondChanged(newValue: Double) {
        if (num2.value != newValue) {
            num2.value = newValue
        }
    }

    private companion object {
        private fun <A, B, R> LiveData<A>.combineWith(b: LiveData<B>, combine: (A?, B?) -> R?): LiveData<R> =
            MediatorLiveData<R>().apply {
                var lastA: A? = this@combineWith.value
                var lastB: B? = b.value

                addSource(this@combineWith) {
                    lastA = it
                    value = combine.invoke(lastA, lastB)
                }

                addSource(b) {
                    lastB = it
                    value = combine.invoke(lastA, lastB)
                }
            }
    }
}

Post a Comment for "Android Livedata Not Updating Edittext"