如何在 Android Studio 中使用 Kotlin 检索在 onItemSelected 函数中修改的变量的值?
How to retrieve the value of a variable modified in the onItemSelected function with Kotlin in Android Studio?
我需要使用 when 检索在 onItemSelected
函数中修改的变量的值。
这是我所做的:
(...)
private var i: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
(...)
categorySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
@SuppressLint("SetTextI18n")
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
category = categories[position]
when (category) {
"Décès" -> i = 1
"Habitat" -> i = 2
}
}
}
binding.tvtest.text = i.toString()
val acteSpinner: Spinner = binding.acteCreatingSpinnerActes
when (i) {
1 -> {
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(deces)])
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
}
2 -> {
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(habitat)])
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
}
}
}
}
我试图在布局中检索变量 I 的值,我得到了 i = 0
。
我想得到onItemSelected
函数
中得到的值
- 不应在代码中使用硬字符串:“Décès”、“Habitat”
- 不需要变量。仅使用位置:
when (position) {
0 -> {
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(deces)])
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
}
1 -> {
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(habitat)])
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
} }
在回调中更改 class 成员没有问题,但是您编写的代码不起作用,因为回调不会立即 运行 - 它 运行s 稍后当用户单击某个项目时。
您似乎希望 acteSpinner
的适配器在您 select categorySpinner
中的内容时在两组不同的数据之间切换。您可以通过直接修改适配器中的数据来实现。这是您如何执行此操作的示例。
// You could make adapter a class member, but it is not necessary
// for this example
//private lateinit var adapter: ArrayAdapter<String>
override fun onCreate(savedInstanceState: Bundle?) {
//...
val binding = ...
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
// Initially set the adapter to show list one
adapter.addAll(firstList)
binding.tvtest.text = "List One"
categorySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
@SuppressLint("SetTextI18n")
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
category = categories[position]
// POINT B - Inside the callback. This code does not
// run immediately, it runs later when an
// item is selected
when (category) {
"Décès" -> {
adapter.clear()
adapter.addAll(decesArray)
adapter.notifyDataSetChanged()
binding.tvtest.text = "Décès"
println("TEST: added deces ${decesArray.contentToString()}")
}
"Habitat" -> {
adapter.clear()
adapter.addAll(habitatArray)
adapter.notifyDataSetChanged()
binding.tvtest.text = "Habitat"
println("TEST: added habitat ${habitatArray.contentToString()}")
}
}
}
}
// POINT A - Outside the callback
//...
}
请注意“Point A”和“Point B”注释 - 在此代码中,“Point A”将 运行 首先,即使“Point B”位于较早的行中。如果您尝试使用您在“B 点”修改的变量在“A 点”做某事,它将不起作用。
试试这个
override fun onCreate(savedInstanceState: Bundle?) {
categorySpinner.onItemSelectedListener = object : ItemSelected{
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
category = categories[position]
when (category) {
"Décès" -> getItemSelected(1)
"Habitat" -> getItemSelected(2)
}}}
}
private fun setDataSpinner(i: Int,actes: Actes){
binding.tvtest.text = i.toString()
val acteSpinner: Spinner = binding.acteCreatingSpinnerActes
val adapter = ArrayAdapter(this, R.layout.simple_spinner_item, actes)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
}
private fun getItemSelected(item: Int){
when(item){
1 -> { setDataSpinner(item,actes[actes.indexOf(deces)]) }
2 -> { setDataSpinner(item,actes[actes.indexOf(habitat)]) }
}
}
interface ItemSelected: AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
我需要使用 when 检索在 onItemSelected
函数中修改的变量的值。
这是我所做的:
(...)
private var i: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
(...)
categorySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
@SuppressLint("SetTextI18n")
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
category = categories[position]
when (category) {
"Décès" -> i = 1
"Habitat" -> i = 2
}
}
}
binding.tvtest.text = i.toString()
val acteSpinner: Spinner = binding.acteCreatingSpinnerActes
when (i) {
1 -> {
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(deces)])
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
}
2 -> {
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(habitat)])
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
}
}
}
}
我试图在布局中检索变量 I 的值,我得到了 i = 0
。
我想得到onItemSelected
函数
- 不应在代码中使用硬字符串:“Décès”、“Habitat”
- 不需要变量。仅使用位置:
when (position) {
0 -> {
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(deces)])
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
}
1 -> {
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(habitat)])
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
} }
在回调中更改 class 成员没有问题,但是您编写的代码不起作用,因为回调不会立即 运行 - 它 运行s 稍后当用户单击某个项目时。
您似乎希望 acteSpinner
的适配器在您 select categorySpinner
中的内容时在两组不同的数据之间切换。您可以通过直接修改适配器中的数据来实现。这是您如何执行此操作的示例。
// You could make adapter a class member, but it is not necessary
// for this example
//private lateinit var adapter: ArrayAdapter<String>
override fun onCreate(savedInstanceState: Bundle?) {
//...
val binding = ...
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
// Initially set the adapter to show list one
adapter.addAll(firstList)
binding.tvtest.text = "List One"
categorySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
@SuppressLint("SetTextI18n")
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
category = categories[position]
// POINT B - Inside the callback. This code does not
// run immediately, it runs later when an
// item is selected
when (category) {
"Décès" -> {
adapter.clear()
adapter.addAll(decesArray)
adapter.notifyDataSetChanged()
binding.tvtest.text = "Décès"
println("TEST: added deces ${decesArray.contentToString()}")
}
"Habitat" -> {
adapter.clear()
adapter.addAll(habitatArray)
adapter.notifyDataSetChanged()
binding.tvtest.text = "Habitat"
println("TEST: added habitat ${habitatArray.contentToString()}")
}
}
}
}
// POINT A - Outside the callback
//...
}
请注意“Point A”和“Point B”注释 - 在此代码中,“Point A”将 运行 首先,即使“Point B”位于较早的行中。如果您尝试使用您在“B 点”修改的变量在“A 点”做某事,它将不起作用。
试试这个
override fun onCreate(savedInstanceState: Bundle?) {
categorySpinner.onItemSelectedListener = object : ItemSelected{
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
category = categories[position]
when (category) {
"Décès" -> getItemSelected(1)
"Habitat" -> getItemSelected(2)
}}}
}
private fun setDataSpinner(i: Int,actes: Actes){
binding.tvtest.text = i.toString()
val acteSpinner: Spinner = binding.acteCreatingSpinnerActes
val adapter = ArrayAdapter(this, R.layout.simple_spinner_item, actes)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
acteSpinner.adapter = adapter
acte = acteSpinner.selectedItem.toString()
}
private fun getItemSelected(item: Int){
when(item){
1 -> { setDataSpinner(item,actes[actes.indexOf(deces)]) }
2 -> { setDataSpinner(item,actes[actes.indexOf(habitat)]) }
}
}
interface ItemSelected: AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
override fun onNothingSelected(parent: AdapterView<*>?) {}
}