xml 文件定义
<Spinner
android:id="@+id/spinnerType"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/create_a_margin_top"
app:layout_constraintBottom_toBottomOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView4" />
//赋值
val spinnerAdapter = ArrayAdapter<Pair>(
requireContext(),
android.R.layout.simple_spinner_item,
list.map { Pair(it.id,it.typeName) }.toList()
)
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinnerType.adapter = spinnerAdapter
....
//获取数据
val pair = spinnerType.selectedItem as Pair
ac.typeName=pair.value
ac.typeId = pair.key
Pair.kt
data class Pair(val key:Int,val value:String){
override fun toString(): String {
return value
}
}