Skip to content

Instantly share code, notes, and snippets.

@PamilerinId
Created February 15, 2020 07:31
Show Gist options
  • Save PamilerinId/108a056860bb722a7982296161a80b8e to your computer and use it in GitHub Desktop.
Save PamilerinId/108a056860bb722a7982296161a80b8e to your computer and use it in GitHub Desktop.
calculate amount on quantity input
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setupAppbar()
val symbol = intent.getStringExtra(SYMBOL_KEY) ?: throw IllegalArgumentException()
isSellForm = intent.getBooleanExtra(SELL_KEY, false)
buyStockButton.setText(if (isSellForm) R.string.action_sell_stock else R.string.action_buy_stock)
categoryStockViewModel.tags = symbol
radioButtonByText = indexRadioButtonChildren()
fetchContent()
amountRadioGroup.setOnCheckedChangeListener { group: RadioGroup?, checkedId: Int ->
onCheckedChange(group, checkedId)
}
amountEditText.addTextChangedListener {
amountChanged(it.toString())
}
buyStockButton.setBlockingOnClickListener {
buyStockClick()
}
viewModel.preOrderStockTransaction.observe(this, Observer { resource ->
preOrderStockResource(resource)
})
amountEditText.addTextChangedListener {
amountErrorTextView.text = ""
val amount = it.toString().toDoubleOrNull()
if (amount != null) {
quantityEditText.setText(String.format("%.4f", (amount / askPrice)))
} else {
quantityEditText.setText("")
}
}
quantityEditText.addTextChangedListener {
quantityErrorTextView.text = ""
//Hangs for some reason
val quantity = it.toString().toDoubleOrNull()
if (quantity != null) {
println((quantity * askPrice))
amountEditText.setText(String.format("%.4f", (quantity * askPrice)))
} else {
amountEditText.setText("")
}
}
priceEditText.addTextChangedListener {
priceErrorTextView.text = ""
}
orderTypeRadioGroup.setOnCheckedChangeListener { _: RadioGroup?, checkedId: Int ->
orderTypeChanged(checkedId)
}
orderTypeChanged(R.id.marketOrderRadioButton)
}
@NezSpencer
Copy link

Line 41 and 43 are the reason it hangs. You are setting the text of the editText inside a textChangeListener of the editText so there will be infinite loop as the new text you set will also call the listener which will call setText again and so on.

Suggested fix:

  1. create a variable to hold the textChangedListener of :
    I. AmountEditeText
    ii. QuantityEditText
    I. e
val amountListener = ...
val quantityListenr = ... {
}
amountEditText.addTextChangedListener {
            amountErrorTextView.text = ""
            val amount = it.toString().toDoubleOrNull()
            quantityEditText.removeTextChangeListener(quantityListener)
            if (amount != null) {
                quantityEditText.setText(String.format("%.4f", (amount / askPrice)))
            } else {
                quantityEditText.setText("")
            }
           quantityEditText.addTextChangeListener(quantityListener)
        }
        quantityEditText.addTextChangedListener {
            quantityErrorTextView.text = ""
            //Hangs for some reason
            val quantity = it.toString().toDoubleOrNull()
                quantityEditText.removeTextChangeListener(amountListener)
            if (quantity != null) {
                println((quantity * askPrice))
                amountEditText.setText(String.format("%.4f", (quantity * askPrice)))
            } else {
                amountEditText.setText("")
            }
                amountEditText.addTextChangeListener(amountListener)
        } 

@NezSpencer
Copy link

val amountListener = object : TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
               amountErrorTextView.text = ""
            val amount = it.toString().toDoubleOrNull()

            if (amount != null) {
                quantityEditText.setText(String.format("%.4f", (amount / askPrice)))
            } else {
                quantityEditText.setText("")
            }
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
        }

val quantityListener = object : TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
                quantityErrorTextView.text = ""
            //Hangs for some reason
            val quantity = it.toString().toDoubleOrNull()
            if (quantity != null) {
                println((quantity * askPrice))
                amountEditText.setText(String.format("%.4f", (quantity * askPrice)))
            } else {
                amountEditText.setText("")
            }
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
        }

@NezSpencer
Copy link

so this:

amountEditText.addTextChangedListener {
            amountErrorTextView.text = ""
            val amount = it.toString().toDoubleOrNull()

            if (amount != null) {
                quantityEditText.setText(String.format("%.4f", (amount / askPrice)))
            } else {
                quantityEditText.setText("")
            }
        }

changes to
amountEditText.addTextChangedListener(amountListener)
and so on.

@PamilerinId
Copy link
Author

PamilerinId commented Feb 15, 2020

heres what i got currently:

 val amountListener = object : TextWatcher{
        override fun afterTextChanged(p0: Editable?) {
            amountErrorTextView.text = ""
            val amount = p0.toString().toDoubleOrNull()
            // Obviuosly enters a recursive problem
//           quantityEditText.removeTextChangedListener(quantityListener)
            if (amount != null) {
                quantityEditText.setText(String.format("%.4f", (amount / askPrice)))
            } else {
                quantityEditText.setText("")
            }
        }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
    }

    val quantityListener = object :  TextWatcher {
        override fun afterTextChanged(p0: Editable?) {
            quantityErrorTextView.text = ""

            val quantity = p0.toString().toDoubleOrNull()
            //remove listener
            amountEditText.removeTextChangedListener(amountListener)
            if (quantity != null) {
                println((quantity * askPrice))
                amountEditText.setText(String.format("%.4f", (quantity * askPrice)))
            } else {
                amountEditText.setText("")
            }
            //add listener
            amountEditText.addTextChangedListener(amountListener)
        }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
    }




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setupAppbar()

        val symbol = intent.getStringExtra(SYMBOL_KEY) ?: throw IllegalArgumentException()
        isSellForm = intent.getBooleanExtra(SELL_KEY, false)
        buyStockButton.setText(if (isSellForm) R.string.action_sell_stock else R.string.action_buy_stock)
        categoryStockViewModel.tags = symbol
        radioButtonByText = indexRadioButtonChildren()
        fetchContent()

        amountRadioGroup.setOnCheckedChangeListener { group: RadioGroup?, checkedId: Int ->
            onCheckedChange(group, checkedId)
        }
        amountEditText.addTextChangedListener {
            amountChanged(it.toString())
        }
        buyStockButton.setBlockingOnClickListener {
            buyStockClick()
        }

        viewModel.preOrderStockTransaction.observe(this, Observer { resource ->
            preOrderStockResource(resource)
        })


        amountEditText.addTextChangedListener(amountListener)
        quantityEditText.addTextChangedListener(quantityListener)

@NezSpencer
Copy link

Remove this line:

amountEditText.addTextChangedListener {
amountChanged(it.toString())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment