Kotlin Spinner sselectedItem 显示对象而不是文本
Kotlin Spinner sselectedItem showing object not text
我已经为微调器创建了一个适配器,getDropDownView
正确显示了自定义对象。但是在选择项目之后,它不会显示该字符串,而是显示完整的对象。任何帮助将不胜感激。
所以不显示:
"(${item?.code}) ${item?.name}"
eg. (Testing) Test
它只是显示
Depot(id=4, name=Test, code=Testing, status_label=Error)
微调器XML
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/depot_drop_down_container"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/userDisplayName"
android:hint="Select Your Depot"
android:orientation="horizontal"
>
<AutoCompleteTextView
android:id="@+id/depot_drop_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>
旋转器适配器
class DepotSpinnerAdapter(
context: Context, textViewResourceId: Int,
values: List<Depot?>?
) :
ArrayAdapter<Depot?>(context, textViewResourceId, values!!) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val label = super.getView(position, convertView, parent!!) as TextView
label.setTextColor(Color.BLACK)
val item = getItem(position)
label.text = "(${item?.code}) ${item?.name}"
return label
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View {
val label = super.getView(position, convertView, parent!!) as TextView
label.setTextColor(Color.BLACK)
val item = getItem(position)
label.text = "(${item?.code}) ${item?.name}"
return label
}
}
事实证明这是一个非常简单的解决方案。我的 Depot
数据 class 需要覆盖 toString
方法。
override fun toString(): String {
return "($code) $name"
}
完成它看起来像这样:
data class Depot (
val id: Long,
val name: String,
val code: String,
val status_label: String
) {
override fun toString(): String {
return "($code) $name"
}
}
在此处找到答案:
我已经为微调器创建了一个适配器,getDropDownView
正确显示了自定义对象。但是在选择项目之后,它不会显示该字符串,而是显示完整的对象。任何帮助将不胜感激。
所以不显示:
"(${item?.code}) ${item?.name}"
eg. (Testing) Test
它只是显示
Depot(id=4, name=Test, code=Testing, status_label=Error)
微调器XML
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/depot_drop_down_container"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/userDisplayName"
android:hint="Select Your Depot"
android:orientation="horizontal"
>
<AutoCompleteTextView
android:id="@+id/depot_drop_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>
旋转器适配器
class DepotSpinnerAdapter(
context: Context, textViewResourceId: Int,
values: List<Depot?>?
) :
ArrayAdapter<Depot?>(context, textViewResourceId, values!!) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val label = super.getView(position, convertView, parent!!) as TextView
label.setTextColor(Color.BLACK)
val item = getItem(position)
label.text = "(${item?.code}) ${item?.name}"
return label
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View {
val label = super.getView(position, convertView, parent!!) as TextView
label.setTextColor(Color.BLACK)
val item = getItem(position)
label.text = "(${item?.code}) ${item?.name}"
return label
}
}
事实证明这是一个非常简单的解决方案。我的 Depot
数据 class 需要覆盖 toString
方法。
override fun toString(): String {
return "($code) $name"
}
完成它看起来像这样:
data class Depot (
val id: Long,
val name: String,
val code: String,
val status_label: String
) {
override fun toString(): String {
return "($code) $name"
}
}
在此处找到答案: