单击微调器时删除微调器内的项目
Remove item inside spinner when spinner is clicked
在 MainActivity
中,它有一个 spinner
,其中项目是使用 Coroutine
从服务器获取的
代码
fun getList() {
val service = RetrofitFactory.makeRetrofitService()
GlobalScope.launch(Dispatchers.Main) {
val request = WebApi.getList(context)
request?.userName.let {
for (i in it!!.iterator()) {
list.put("", "")
list.put("None","None")
list[i.name!!] = i.id!! // Ali
}
spinnerName?.let { spn ->
spn.adapter = ArrayAdapter(context, R.layout.spinner_item, list.keys.toTypedArray())
val position = (spn.adapter as ArrayAdapter<String>).getPosition("")
spn.setSelection(position)
}
}
}
}
首先,我希望微调器默认显示 empty
。当用户单击微调器时,它只会显示 两项 ,即 'None' 和 'Ali'。
我怎样才能做到这一点?
到目前为止,我可以将空字符串设置为默认值,但是当我单击时,微调器中有 3 个项目,分别是 None、Ali 和空字符串。
要使您的 empty
成为默认值但不在 下拉列表 中,您可以提供包含您的 empty
实体的适配器列表结束,但对于 适配器计数 ,在 getCount()
方法中少提供一个计数。这样下拉列表中的最后一个条目将被忽略。
看看如何做到:
val list = arrayListOf("None","Ali","Empty") // You can provide any type of list here
val adapter = object : ArrayAdapter<String>(context, itemLayout, list) {
//Override getCount method and we reduce one count less when list is there, so that last entry would be ignored in dropdown.
override fun getCount(): Int {
val count = super.getCount()
return if (count > 0) count - 1 else count
}
}
your_spinner.adapter = adapter
your_spinner.setSelection(list.size-1) // We make our last list item as default entry
编辑 O.P。:
spinnerName?.let { spn ->
val adapterList = list.keys.toMutableList().apply { this.add("Empty") }
spn.adapter = object : ArrayAdapter<String>(context, R.layout.spinner_item, adapterList) {
//Override getCount method and we reduce one count less when list is there, so that last entry would be ignored in dropdown.
override fun getCount(): Int {
val count = super.getCount()
return if (count > 0) count - 1 else count
}
}
spn.setSelection(adapterList.size - 1)
}
在 MainActivity
中,它有一个 spinner
,其中项目是使用 Coroutine
代码
fun getList() {
val service = RetrofitFactory.makeRetrofitService()
GlobalScope.launch(Dispatchers.Main) {
val request = WebApi.getList(context)
request?.userName.let {
for (i in it!!.iterator()) {
list.put("", "")
list.put("None","None")
list[i.name!!] = i.id!! // Ali
}
spinnerName?.let { spn ->
spn.adapter = ArrayAdapter(context, R.layout.spinner_item, list.keys.toTypedArray())
val position = (spn.adapter as ArrayAdapter<String>).getPosition("")
spn.setSelection(position)
}
}
}
}
首先,我希望微调器默认显示 empty
。当用户单击微调器时,它只会显示 两项 ,即 'None' 和 'Ali'。
我怎样才能做到这一点?
到目前为止,我可以将空字符串设置为默认值,但是当我单击时,微调器中有 3 个项目,分别是 None、Ali 和空字符串。
要使您的 empty
成为默认值但不在 下拉列表 中,您可以提供包含您的 empty
实体的适配器列表结束,但对于 适配器计数 ,在 getCount()
方法中少提供一个计数。这样下拉列表中的最后一个条目将被忽略。
看看如何做到:
val list = arrayListOf("None","Ali","Empty") // You can provide any type of list here
val adapter = object : ArrayAdapter<String>(context, itemLayout, list) {
//Override getCount method and we reduce one count less when list is there, so that last entry would be ignored in dropdown.
override fun getCount(): Int {
val count = super.getCount()
return if (count > 0) count - 1 else count
}
}
your_spinner.adapter = adapter
your_spinner.setSelection(list.size-1) // We make our last list item as default entry
编辑 O.P。:
spinnerName?.let { spn ->
val adapterList = list.keys.toMutableList().apply { this.add("Empty") }
spn.adapter = object : ArrayAdapter<String>(context, R.layout.spinner_item, adapterList) {
//Override getCount method and we reduce one count less when list is there, so that last entry would be ignored in dropdown.
override fun getCount(): Int {
val count = super.getCount()
return if (count > 0) count - 1 else count
}
}
spn.setSelection(adapterList.size - 1)
}