编程更改Spinner文本内容

发布于 2025-01-28 18:38:56 字数 628 浏览 3 评论 0原文

我有一个旋转器,我想找到一种方法来更改旋转器的下拉菜单。我想通过代码更改文本内容,但我不确定该怎么做。

这是我的旋转器的结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2025-02-04 18:38:56

如果要更改列表,则适配器正在显示您可以使用clear> clearaddall来更改数据。

对于这种情况,您将要确保不要将列表传递给
适配器的构造函数,否则调用clearaddall将尝试修改原始列表。如果原来
列表是不变的(listof)会丢弃错误,如果它是可变的(arraylist),则可能会得到意外的行为
因为它将修改原始源列表。

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private val arr1 = listOf("1","2","3")
    private val arr2 = listOf("a","b","c")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // If you pass an array in here, calling things like "clear"
        // or "addAll" will modify the original list passed in. Passing in
        // nothing means the adapter will hold its own unique list...
        val adapter = ArrayAdapter<String>(this, R.layout.simple_spinner_item)

        // ...and you can initialize the adapter's list using addAll
        adapter.addAll(arr1)

        binding.mySpinner.adapter = adapter

        binding.changeSpinnerText.setOnClickListener {
            // to change what is shown, clear the adapter's list
            // and add the new values to show

            adapter.clear() // remove the existing values
            adapter.addAll(arr2) // add the new values
        }
    }
}

If you want to change the list your adapter is showing you can use clear and addAll 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 or addAll will attempt to modify the original list. If the original
list is immutable (listOf) it will throw an error, and if it is mutable (ArrayList) you may get unexpected behavior
since it will modify the original source list.

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private val arr1 = listOf("1","2","3")
    private val arr2 = listOf("a","b","c")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // If you pass an array in here, calling things like "clear"
        // or "addAll" will modify the original list passed in. Passing in
        // nothing means the adapter will hold its own unique list...
        val adapter = ArrayAdapter<String>(this, R.layout.simple_spinner_item)

        // ...and you can initialize the adapter's list using addAll
        adapter.addAll(arr1)

        binding.mySpinner.adapter = adapter

        binding.changeSpinnerText.setOnClickListener {
            // to change what is shown, clear the adapter's list
            // and add the new values to show

            adapter.clear() // remove the existing values
            adapter.addAll(arr2) // add the new values
        }
    }
}
镜花水月 2025-02-04 18:38:56

使用条件语句并根据条件填充旋转器中使用的数组,然后根据需求设置下拉列表的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文