所以我在这个项目上与Kotlin合作,我主要使用Java,所以我试图在Kotlin中找到等价物来实现相同的功能。
我从数据库中获取一个JSONArray,并将其存储在一个名为
Congregation
它有以下变量
id: Int, name: String, language: String
我现在有一个注册活动,其中有一个“会众”的输入,我决定把它作为一个
AutoCompleteTextView
因此,我可以建议与用户键入内容匹配的可能值。我创造了一个习惯
ArrayAdapter
当显示
name
一个
会众
(
(下图)
然而
,当我选择其中一个值时,它会显示列表中的完整值文本
e、 g.如果我在输入中选择“Leeds,Crossgates(英语)”,它将显示
Congregation(id=1, name=Leeds, Crossgates, language=English)
我想知道我怎样才能保住这份工作
名称
a的价值
会众
选中时,将显示在框中。
此外,当我在选择一个项目后尝试删除框的当前值时,我会收到一条消息
IndexOutOfBoundsException Index: 1 Size: 1
自定义阵列适配器(collectionListAdapter.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
}
}
自定义视图(collection_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
)