gpt4 book ai didi

Android Material 下拉列表 : Selected dropdown item is presented like toString instead of defined View

转载 作者:行者123 更新时间:2023-12-02 12:45:17 24 4
gpt4 key购买 nike

我正在尝试为组件 TextInputLayoutAutoCompleteTextView 使用 android Material 样式。我希望所选项目的显示方式与下拉列表中的显示方式相同:图像和文本。

我尝试在 TextInputLayout 中使用 Spinner,在没有 TextInputLayout 的情况下使用 Spinner 和 AutoCompleteTextView。所有组合都有效,但第一个组合无效( Material 样式不适用于这些组合)。

所选项目显示为我用于自定义适配器的对象的 toString。

我做错了什么?

附注:我正在为图像使用 SVG 可绘制对象。

ma​​in_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/language_from"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:hint="from"
app:layout_constraintBottom_toTopOf="@+id/language_to"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

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

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

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/language_to"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:hint="to"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/language_from">

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

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

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/searchInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:hint="label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/language_to">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/searchInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

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

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="412dp"
android:layout_height="531dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/searchInputLayout">

</androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val languageList = createLanguages()
val adapter = LanguageAdapter(this, languageList)

val languageFromDropdown = findViewById<AutoCompleteTextView>(R.id.language_from_view)
val languageToDropdown = findViewById<AutoCompleteTextView>(R.id.language_to_view)

languageFromDropdown.setAdapter(adapter)
languageToDropdown.setAdapter(adapter)
}

private fun createLanguages(): List<LanguageDropdownItem>
= listOf(LanguageDropdownItem(Language.POLISH, R.drawable.ic_polish_flag),
LanguageDropdownItem(Language.ENGLISH, R.drawable.ic_english_flag),
LanguageDropdownItem(Language.GERMAN, R.drawable.ic_german_flag),
LanguageDropdownItem(Language.FRENCH, R.drawable.ic_french_flag),
LanguageDropdownItem(Language.ITALIAN, R.drawable.ic_italian_flag),
LanguageDropdownItem(Language.SWISS, R.drawable.ic_swiss_flag))

}

language_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/flag_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="2dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_english_flag" />

<TextView
android:id="@+id/language_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/flag_image"
app:layout_constraintTop_toTopOf="parent"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1" />


</androidx.constraintlayout.widget.ConstraintLayout>

LanguageDropdownItem.kt

data class LanguageDropdownItem(val language: Language, val drawableFlag: Int)

LanguageAdapter.kt

class LanguageAdapter(val mContext: Context,
val mLanguages: List<LanguageDropdownItem>)
: ArrayAdapter<LanguageDropdownItem>(mContext, 0, mLanguages) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var dropdownItemView = convertView

if (dropdownItemView == null)
dropdownItemView = LayoutInflater.from(mContext)
.inflate(R.layout.language_dropdown_item, parent,false)

val language = mLanguages[position]

val flagImage: ImageView = dropdownItemView!!.findViewById(R.id.flag_image)
flagImage.setImageResource(language.drawableFlag)

val langTextView: TextView = dropdownItemView.findViewById(R.id.language_name)
langTextView.text = language.language.name

return dropdownItemView
}
}

Language 是带有语言名称的枚举类。

screenshot of my dropdown and selected result

最佳答案

如果您重写 LanguageDropdownItem 的 toString() 函数,您将实现您想要的...

注意:我的问题是这样解决的。

data class LanguageDropdownItem(val language: Language, val drawableFlag: Int) {
override fun toString(): String {
TODO()
}
}

关于Android Material 下拉列表 : Selected dropdown item is presented like toString instead of defined View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61156453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com