ArrayAdapter OnItemSelectedListener 不起作用

问题描述

我创建了一个 arrayAdapter 来显示 MaterialDropdownMenu 中的项目,我希望 TextView 将文本设置为所选项目,但文本永远不会改变。为什么不这样做? 谢谢

这里是 Kotlin 文件

    val options = arrayOf("Item 1","Item 2","Item 3")
    val arrayAdapter = ArrayAdapter(this,R.layout.options_slider,options)
    autoOptions.setText(arrayAdapter.getItem(0).toString(),false)
    autoOptions.setAdapter(arrayAdapter)

    autoOptions.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
        override fun onnothingSelected(parent: AdapterView<*>?) {

        }

        override fun onItemSelected(parent: AdapterView<*>?,view: View?,position: Int,id: Long) {

            tv1.text=options[position]
        }

    }

和xml文件

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/menu"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Settings">

        <AutoCompleteTextView
            android:id="@+id/autoOptions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="none"
            />

        <TextView
        android:id="@+id/tv1"
        android:layout_width="300dp"
        android:layout_height="25dp"
        android:text="test"
        android:textAlignment="center">

    </TextView>

    </com.google.android.material.textfield.TextInputLayout>

这里是 options_slider 的 xml 文件

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:padding="16dp"
    android:maxLines="1"
    android:textAppearance="?attr/textAppearanceSubtitle1">

</TextView>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...