Created
February 15, 2020 07:31
-
-
Save PamilerinId/108a056860bb722a7982296161a80b8e to your computer and use it in GitHub Desktop.
calculate amount on quantity input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
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) {}
}
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.
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)
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
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:
I. AmountEditeText
ii. QuantityEditText
I. e