编程更改Spinner文本内容
我有一个旋转器,我想找到一种方法来更改旋转器的下拉菜单。我想通过代码更改文本内容,但我不确定该怎么做。
这是我的旋转器的结构:
val adapter: ArrayAdapter<String> = ArrayAdapter<String>(
context,
android.R.layout.simple_spinner_item, units1[i]
)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
val new_spin = Spinner(context)
new_spin.setAdapter(adapter)
我想将旋转器中的文本更改为其他列表(unit2)。
I have a spinner, and I want to find a way to change the dropdown for my spinner. I want to change my text content through code, but I'm not sure how to do that.
This is the structure of my Spinner:
val adapter: ArrayAdapter<String> = ArrayAdapter<String>(
context,
android.R.layout.simple_spinner_item, units1[i]
)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
val new_spin = Spinner(context)
new_spin.setAdapter(adapter)
I want to change the text in my spinner to a different list (unit2).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要更改列表,则适配器正在显示您可以使用
clear> clear
和addall
来更改数据。对于这种情况,您将要确保不要将列表传递给
适配器的构造函数,否则调用
clear
或addall
将尝试修改原始列表。如果原来列表是不变的(
listof
)会丢弃错误,如果它是可变的(arraylist
),则可能会得到意外的行为因为它将修改原始源列表。
If you want to change the list your adapter is showing you can use
clear
andaddAll
to change the data.For this case you will want to make sure not to pass the list in to the
constructor of the adapter, otherwise calling
clear
oraddAll
will attempt to modify the original list. If the originallist is immutable (
listOf
) it will throw an error, and if it is mutable (ArrayList
) you may get unexpected behaviorsince it will modify the original source list.
使用条件语句并根据条件填充旋转器中使用的数组,然后根据需求设置下拉列表的内容。
Use conditional statements and populate the array being used in spinner as per the condition then you can set the contents of the dropdown as per your demand.