i am trying to do multiple edittext addtextchangedlistener but app crashes

128 Views Asked by At

I have 2 edittexts in my application. When I start to write a value in any of these 2 edittexes, I want to see the result in the other. But when I start typing value in any edittex I get an error

private fun initListener() {
        var money = 0.0
        binding.apply {
            monayValueEditText1.addTextChangedListener {
                val money1position = moneyValueSpinner1.selectedItemPosition
                val money2position = moneyValueSpinner2.selectedItemPosition
                money = if (it!!.isEmpty()) 0.0 else
                    it.toString().toDouble()
                val result =
                    ((currencyList[money1position].ForexBuying!! / currencyList[money1position].Unit!!.toDouble()) * money) / currencyList[money2position].ForexBuying!! / currencyList[money2position].Unit!!.toDouble()
                monayValueEditText2.setText(DecimalFormat("##.####").format(result).toString())
            }
            monayValueEditText2.addTextChangedListener {
                money = if (it!!.isEmpty()) 0.0 else
                    it.toString().toDouble()
                val money1position = moneyValueSpinner1.selectedItemPosition
                val money2position = moneyValueSpinner2.selectedItemPosition
                val result =
                    ((currencyList[money2position].ForexBuying!! / currencyList[money2position].Unit!!.toDouble()) * money) / currencyList[money1position].ForexBuying!! / currencyList[money1position].Unit!!.toDouble()
                monayValueEditText1.setText(DecimalFormat("##.####").format(result).toString())
            }

        }

    }

the error message i get is like this hundreds of

xt(TextView.java:6147)
        at android.widget.EditText.setText(EditText.java:121)
        at android.widget.TextView.setText(TextView.java:6099)
        at com.metoer.ceptedovizborsa.view.fragment.CallculationCurrencyFragment$initListener$lambda-3$$inlined$addTextChangedListener$default$2.afterTextChanged(TextView.kt:104)
        at android.widget.TextView.sendAfterTextChanged(TextView.java:10602)
        at android.widget.TextView.setText(TextView.java:6328)
        at android.widget.TextView.setText(TextView.java:6147)
        at android.widget.EditText.setText(EditText.java:121)
        at android.widget.TextView.setText(TextView.java:6099)
1

There are 1 best solutions below

14
Onur D. On

If you're only interested in getting two EditTexts that work together, I'm thinking you could do it the Two-way databinding way in the XML and keep the text in a ViewModel instead.

<layout ...>

  <data>

      <variable 
          name="viewModel"
          type="com.mypackage.MyViewModel"

  </data>

  ...

  <EditText
       android:id="@+id/monayValueEditText1"
       android:text="@={viewModel.text}"
       ... />

  <EditText
       android:id="@+id/monayValueEditText2"
       android:text="@={viewModel.text}"
       ... />

</layout>