Можно ли обновить textView, используя TextWatcher и спиннер? - PullRequest
0 голосов
/ 15 февраля 2020

Моей целью было создать конвертер валют. После того, как страны выбраны в обоих счетчиках, я получаю, что textView будет обновляться тем, что вводится в textEdit с использованием TextWatcher. Однако, если по какой-либо причине я захочу изменить страну в любом из вращателей, textView не обновляется, чтобы отразить новое преобразование. Я нашел свой подход, но так как я новичок в Kotlin, я чувствую, что этот путь - не лучший вариант.

 class MainActivity : AppCompatActivity() {
        var country1=0
        var country2=0
        var currencyConvertor=CurrencyClass(0.0,country1,country2)
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            inputText.addTextChangedListener(myWatcher())
            spinnerFrom.adapter=ArrayAdapter.createFromResource(this,R.array.menu_items,R.layout.support_simple_spinner_dropdown_item)
            spinnerTo.adapter=ArrayAdapter.createFromResource(this,R.array.menu_items,R.layout.support_simple_spinner_dropdown_item)
            spinnerFrom.onItemSelectedListener=object:AdapterView.OnItemSelectedListener{
                override fun onNothingSelected(parent: AdapterView<*>?) {
                }
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    Toast.makeText(this@MainActivity,spinnerFrom.getItemAtPosition(position).toString(),
                        Toast.LENGTH_LONG).show()
                    if (position==0){
                        flagFrom.setImageResource(R.drawable.usflag)
                        country1=0
                        currencyConvertor.countryFrom=country1
                        textView.text=currencyConvertor.calc().toString()
                    }
                    else if(position==1){
                        flagFrom.setImageResource(R.drawable.canadianflag)
                        country1=1
                        currencyConvertor.countryFrom=country1
                        textView.text=currencyConvertor.calc().toString()
                    }
                    else if (position== 2){
                        flagFrom.setImageResource(R.drawable.mexicanflag)
                        country1=2
                        currencyConvertor.countryFrom=country1
                        textView.text=currencyConvertor.calc().toString()
                    }
                    else if (position== 3){
                        flagFrom.setImageResource(R.drawable.euflag)
                        country1=3
                        currencyConvertor.countryFrom=country1
                        textView.text=currencyConvertor.calc().toString()
                    }
                    else if (position== 4){
                        flagFrom.setImageResource(R.drawable.chineseflag)
                        country1=4
                        currencyConvertor.countryFrom=country1
                        textView.text=currencyConvertor.calc().toString()
                    }
                }
            }
            spinnerTo.onItemSelectedListener = object:AdapterView.OnItemSelectedListener{
                override fun onNothingSelected(parent: AdapterView<*>?) {
                }
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    if (position==0){
                        flagTo.setImageResource(R.drawable.usflag)
                        country2=0
                        currencyConvertor.countryTo=country2
                        textView.text=currencyConvertor.calc().toString()
                    }
                    else if(position==1){
                        flagTo.setImageResource(R.drawable.canadianflag)
                        country2=1
                        currencyConvertor.countryTo=country2
                        textView.text=currencyConvertor.calc().toString()
                    }
                    else if (position== 2){
                        flagTo.setImageResource(R.drawable.mexicanflag)
                        country2=2
                        currencyConvertor.countryTo=country2
                        textView.text=currencyConvertor.calc().toString()
                    }
                    else if (position== 3){
                        flagTo.setImageResource(R.drawable.euflag)
                        country2=3
                        currencyConvertor.countryTo=country2
                        textView.text=currencyConvertor.calc().toString()
                    }
                    else if (position== 4){
                        flagTo.setImageResource(R.drawable.chineseflag)
                        country2=4
                        currencyConvertor.countryTo=country2
                        textView.text=currencyConvertor.calc().toString()
                    }
                }
            }
        }
        inner class myWatcher: TextWatcher{
            override fun afterTextChanged(s: Editable?){
                if (inputText.text.isNotEmpty()){
                    currencyConvertor.countryFrom=country1
                    currencyConvertor.countryTo=country2
                    currencyConvertor.inputAmount=inputText.text.toString().toDouble()
                    var convertedC=currencyConvertor.calc()
                    textView.text=convertedC.toString()
                }
            }
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        } }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...