如何解决AutoCompleteTextView对于Android中的子字符串无法正常工作
AutoCompleteTextView对于Android中的子字符串无法正常工作 我在xml文件中有一个这样的AutoCompleteTextView
<androidx.constraintlayout.widget.ConstraintLayout 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">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="@android:color/transparent"
android:hint="Symbol"
app:boxBackgroundColor="@android:color/transparent"
app:endIconMode="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<AutoCompleteTextView
android:id="@+id/etACSymbol"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:completionThreshold="1"
/>
</com.google.android.material.textfield.TextInputLayout>
.....
</androidx.constraintlayout.widget.ConstraintLayout>
然后我在BottomSheetDialog中像这样配置它
adapterFrom = ArrayAdapter(this,R.layout.spinner_single,symbols)
(dialog.etACSymbol as? AutoCompleteTextView)?.setAdapter(adapterFrom)
(dialog.etACSymbol as? AutoCompleteTextView)?.setOnItemClickListener { adapterView,view,position,l ->
dialog.etACSymbol.text = null
val selected = adapterView.getItemAtPosition(position) as String
dialog.etACSymbol.setText(selected)
println("selected symbol $selected")
SYMBOL = selected.substringAfter("(").substringBefore(")")
price = priceList.get(selected)?.replace(",","")?.toDouble()!!
}
symbols
变量的设置是这样的,我从远程服务器获取数据,然后这样设置
var cWiths = it.companyName.toString()+"("+it.symbol.toString()+")"
symbols.add(cWiths)
然后我得到这样的打字字母
dialog.etACSymbol.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?,p1: Int,p2: Int,p3: Int) {
}
override fun onTextChanged(p0: CharSequence?,p3: Int) {
if (p0 != null && p0.length > 1) {
filterSymbol(p0.toString())
}
}
})
最后,filterSymbol
方法就是这样
fun filterSymbol(prefix: String) {
println("prefix $prefix and size of symbols ${symbols.size}")
symbols.forEach {
if (it.contains(prefix,ignoreCase = true)) {
println("found symbol $it")
adapterFrom.filter.filter(it)
}
}
}
问题是,每当我输入Apple
时,它就可以正常工作并在下拉列表中显示过滤后的值,例如Apple Inc(AAPL)
,但是每当我仅输入AAPL
时,它就会打印日志found sybmol Apple Inc(AAPL)
,但未显示在下拉列表中。
我不知道自己缺少什么,所以我正在寻找另一只眼睛。 任何帮助,将不胜感激。谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。