繁体   English   中英

Kotlin AutoCompleteTextView Adapter Item Selected 提供不同的值

[英]Kotlin AutoCompleteTextView Adapter Item Selected Giving Different Value

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

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

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

显示 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
)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM