问题描述
所以我在这个项目上与 Kotlin 合作,我主要使用 Java,所以我试图在 Kotlin 中找到等价物来实现相同的功能.
我从我的数据库中获取一个 JSONArray 并将其存储在名为 Congregation
的可序列化数据类中,它具有以下变量 id: Int, name: String, language: String
我现在有一个注册活动,其中有一个Congregation"输入,我决定将其设为 AutoCompleteTextView
,以便我可以建议与用户输入的内容相匹配的可能值.我创建了一个自定义的 ArrayAdapter
,它在显示 Congregation
(下图) 的name
时工作正常>
但是,当我选择这些值之一时,它会显示列表中的完整值文本
例如如果我在输入中选择Leeds, Crossgates (English)",它将显示 Congregation(id=1, name=Leeds, Crossgates, language=English)
我想知道如何在选择时将 Congregation
的 name
值保留在框中.
此外,当我在选择项目后尝试删除框的当前值时,我收到 IndexOutOfBoundsException Index: 1 Size: 1
自定义数组适配器 (CongregationListAdapter.kt):
class CongregationListAdapter(context: Activity, resourceId: Int, textView: Int, private var congregations: List):ArrayAdapter(context, resourceId, textView, congregations) {私有变量充气器:LayoutInflater = context.layoutInflater私有 lateinit var 视图:查看@SuppressLint("SetTextI18n")覆盖 fun getView(position: Int, convertView: View?, parent: ViewGroup): View {if (convertView == null) {视图 = inflater.inflate(R.layout.congregation_list_item, parent, false)}val 名称:TextView = view.findViewById(R.id.congregation_text)val 会众:会众?= 获取项目(位置)如果(会众!= null){name.text = congregation.name + " (" + congregation.language + ")"} 别的 {名称.文本 = ""返回视图}返回视图}覆盖乐趣 getCount(): Int {返回 congregations.size}@SuppressLint("SetTextI18n")覆盖 fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {if (convertView == null) {视图 = inflater.inflate(R.layout.congregation_list_item, parent, false)}val 会众:会众?= 获取项目(位置)val 名称:TextView = view.findViewById(R.id.congregation_text)如果(会众!= null){name.text = congregation.name + " (" + congregation.language + ")"} 别的 {name.text = "糟糕.有问题"}返回视图}}
自定义视图 (congregation_list_item.:
<android.support.constraint.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><文本视图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"机器人:文本样式=正常"应用程序:layout_constraintEnd_toEndOf="父"应用程序:layout_constraintHorizontal_bias="0.0"应用程序:layout_constraintStart_toStartOf="parent"应用程序:layout_constraintTop_toTopOf="父"工具:忽略 =MissingConstraints"工具:text="Leeds, Crossgates"/></android.support.constraint.ConstraintLayout></LinearLayout>
会众模型 (Congregation.kt):
@kotlinx.serialization.Serializable数据类 Congregation(变量 ID:整数,变量名称:字符串,var 语言:字符串)
你可以简单地在 Congregation
类中实现 toString
:
@kotlinx.serialization.Serializable数据类 Congregation(变量 ID:整数,变量名称:字符串,var 语言:字符串){覆盖乐趣 toString(): String {返回 "$name ($language)";}}
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.
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
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
e.g. If I select "Leeds, Crossgates (English)" in the input it will display Congregation(id=1, name=Leeds, Crossgates, language=English)
I am wondering how I can keep the name
value of a Congregation
in the box when it is selected.
Also when I try to delete the current value of the box after selecting an item I receive an IndexOutOfBoundsException Index: 1 Size: 1
Custom Array Adapter (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
}
}
Custom View (congregation_list_item.:
<?
Conregation Model (Congregation.kt):
@kotlinx.serialization.Serializable
data class Congregation(
var id: Int,
var name: String,
var language: String
)
You can simply implement 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)"
}
}
这篇关于Kotlin AutoCompleteTextView Adapter Item Selected 提供不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!