Kotlin AutoCompleteTextView 适配器项目选择给出不同的值

2024-04-28

所以我在这个项目上使用 Kotlin,我主要使用 Java,所以我试图在 Kotlin 中找到等效项来实现相同的功能。

我从数据库中获取 JSONArray 并将其存储在名为的可序列化数据类中Congregation它有以下变量id: Int, name: String, language: String

我现在有一个注册活动,其中有一个“会众”输入,我决定将其作为AutoCompleteTextView这样我就可以建议与用户输入的内容相匹配的可能值。我创建了一个自定义ArrayAdapter并且在显示时工作正常name of a Congregation (下图)

However,当我选择这些值之一时,它会显示列表中的完整值文本

例如如果我在输入中选择“Leeds, Crossgates (English)”,它将显示Congregation(id=1, name=Leeds, Crossgates, language=English)

我想知道如何才能保持name的值Congregation当它被选中时在框中。

此外,当我在选择一个项目后尝试删除该框的当前值时,我收到一个IndexOutOfBoundsException Index: 1 Size: 1

自定义数组适配器 (CongregationListAdapter.kt):

class CongregationListAdapter(context: Activity, resourceId: Int, textView: Int, private var congregations: List<Congregation>)
:ArrayAdapter<Congregation>(context, resourceId, textView, congregations) {

private var inflater: LayoutInflater = context.layoutInflater
private lateinit var view: View

@SuppressLint("SetTextI18n")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

    if (convertView == null) {
        view = inflater.inflate(R.layout.congregation_list_item, parent, false)
    }
    val name: TextView = view.findViewById(R.id.congregation_text)

    val congregation: Congregation? = getItem(position)
    if (congregation != null) {
        name.text = congregation.name + " (" + congregation.language + ")"
    } else {
        name.text = ""
        return view
    }
    return view
}

override fun getCount(): Int {
    return congregations.size
}

@SuppressLint("SetTextI18n")
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
    if (convertView == null) {
        view = inflater.inflate(R.layout.congregation_list_item, parent, false)
    }
    val congregation: Congregation? = getItem(position)
    val name: TextView = view.findViewById(R.id.congregation_text)
    if (congregation != null) {
        name.text = congregation.name + " (" + congregation.language + ")"
    } else {
        name.text = "Oops. There was a problem"
    }
    return view
}

}

自定义视图 (congregation_list_item.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/congregation_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="normal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints"
        tools:text="Leeds, Crossgates" />

</android.support.constraint.ConstraintLayout>

</LinearLayout>

聚合模型 (Congregation.kt):

@kotlinx.serialization.Serializable
data class Congregation(
    var id: Int,
    var name: String,
    var language: String
)

您可以简单地实施toString in the Congregation class:

@kotlinx.serialization.Serializable
data class Congregation(
    var id: Int,
    var name: String,
    var language: String
) {
    override fun toString(): String {
        return "$name ($language)"
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Kotlin AutoCompleteTextView 适配器项目选择给出不同的值 的相关文章

随机推荐