选择Kotlin AutoCompleteTextView适配器项以提供不同的值 [英] Kotlin AutoCompleteTextView Adapter Item Selected Giving Different Value

查看:71
本文介绍了选择Kotlin AutoCompleteTextView适配器项以提供不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在与Kotlin一起进行这个项目,我主要使用Java,因此我试图在Kotlin中找到等效项以实现相同的功能.

So I am working with Kotlin on this project, I mainly use Java so I'm trying to find equivalents in Kotlin to achieve the same functionality.

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

I am getting a JSONArray from my Database and storing it in a Serializable data class called Congregation it has the following variables id: Int, name: String, language: String

我现在有一个注册活动,其中有一个会众"输入,我决定将此输入为AutoCompleteTextView,以便我可以建议与用户键入的内容匹配的可能值.我创建了一个自定义ArrayAdapter,当显示Congregationname时(在下面的图片中)

I now have a registration activity in which there is a "Congregation" input, I decided to make this an AutoCompleteTextView so that I could suggest possible values matching what the users typing. I created a custom ArrayAdapter and it works fine when displaying the name of a Congregation (image below)

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

However, when I select one of those values, it displays the full value text from the list

例如如果在输入中选择利兹,十字门(英语)",它将显示Congregation(id=1, name=Leeds, Crossgates, language=English)

我想知道如何在选择Congregation时将Congregationname值保留在框中.

I am wondering how I can keep the name value of a Congregation in the box when it is selected.

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

Also when I try to delete the current value of the box after selecting an item I receive an 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)"
    }
}

这篇关于选择Kotlin AutoCompleteTextView适配器项以提供不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆