How would I get the val selectedCountry
which I believe is local to that function available to use in other functions or is there a way I can get what the current selected value of the spinner is?
I tried to treat it like Pyhton and Java where I would call the funtion and it would return that value but that did not work.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(currencyconvertermain)
val ConvertCurrency = findViewById<Button>(R.id.convertcurrency)
val CurrencyOutput = findViewById<TextView>(R.id.currencyoutput)
val CurrencyInput = findViewById<EditText>(R.id.currencyinput)
val CountrySelect = findViewById<Spinner>(R.id.CountrySelect)
val CountryImage = findViewById<ImageView>(R.id.CurrencyFlag)
val CountrySelectAdapter = ArrayAdapter.createFromResource(this, R.array.dropdown_items, android.R.layout.simple_spinner_dropdown_item,)
CountrySelect.adapter = CountrySelectAdapter
CountrySelect.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parentView: AdapterView<*>,
selectedItemView: View?,
position: Int,
id: Long,
) {
val selectedCountry = parentView.getItemAtPosition(position).toString()
if (selectedCountry == "Japan") {
CountryImage.setImageResource(R.drawable.japan)
}
if (selectedCountry == "Phillipenes") {
CountryImage.setImageResource(R.drawable.philippines)
}
if (selectedCountry == "Nepal") {
CountryImage.setImageResource(R.drawable.nepal)
}
if (selectedCountry == "Vietnam") {
CountryImage.setImageResource(R.drawable.vietnam)
}
if (selectedCountry == "Taiwan") {
CountryImage.setImageResource(R.drawable.taiwan)
}
}
override fun onNothingSelected(parentView: AdapterView<*>) {}
}
}
}
New contributor
Valera Nordstrom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5