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 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) | |
} |
NezSpencer
commented
Feb 15, 2020
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