Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

slider - ArrayAdapter OnItemSelectedListener does not work

I created an arrayAdapter to show items in the MaterialDropdownMenu and i want the TextView to set the text to the item selected, but the text never changes. Why doesen′t it do that? Thanks

Here the Kotlin file:

    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]
        }

    }

and the xml file:

<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>

and here the xml file for options_slider:

<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>
question from:https://stackoverflow.com/questions/65866176/arrayadapter-onitemselectedlistener-does-not-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...