Kotlin中的AutoCompleteTextView适配器选中项给出了不同的值

3

我正在这个项目中使用Kotlin,主要使用Java,因此我正在尝试在Kotlin中找到相应的等效方法以实现相同的功能。

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

现在我有一个注册活动,在其中有一个“Congregation”输入,我决定将其作为AutoCompleteTextView,以便我可以建议与用户输入匹配的可能值。我创建了一个自定义的ArrayAdapter,当显示Congregationname时它可以正常工作(下面是图像)。

Emulator showing RegisterActivity.java

然而,当我选择其中一个值时,它会显示列表中的完整文本。

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

我想知道如何在选择时保留Congregationname值。

此外,在选择项目后尝试删除框中的当前值时,我收到一个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
)

你应该在getView()getDropDownView()中使用局部变量,而不是像你为view所做的那样使用类变量。 - Blo
什么东西需要一个本地变量?抱歉,请问您的意思是? - Mwikala Kangwa
你应该这样做:override getView(...) { val view: View = convertView ?: inflater.inflate(...) },并在最后返回它。对于 getDropDownView() 也是同样的操作。这可以防止一些奇怪的行为。这也将替换你的 if null 检查 ;) - Blo
可以使用getAdapterPosition代替吗? - mochadwi
1个回答

0

您可以在 Congregation 类中简单地实现 toString 方法:

@kotlinx.serialization.Serializable
data class Congregation(
    var id: Int,
    var name: String,
    var language: String
) {
    override fun toString(): String {
        return "$name ($language)"
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接