所以我在这个项目上与 Kotlin 合作,我主要使用 Java,所以我试图在 Kotlin 中找到等价物来实现相同的功能。
我从我的数据库中获取一个 JSONArray 并将其存储在一个名为 Serializable 的数据类中Congregation
,它具有以下变量id: Int, name: String, language: String
我现在有一个注册活动,其中有一个“会众”输入,我决定将其设为一个AutoCompleteTextView
,以便我可以建议与用户键入的内容相匹配的可能值。我创建了一个自定义ArrayAdapter
,它在显示(name
下图)时工作正常Congregation
但是,当我选择其中一个值时,它会显示列表中的完整值文本
例如,如果我在输入中选择“Leeds, Crossgates (English)”,它将显示Congregation(id=1, name=Leeds, Crossgates, language=English)
我想知道如何在选中name
a 时将其值保留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
)