Android 将AutoCompleteTextView筛选器从“更改为”;用“开始”;至;包含“;?
我想更改Android 将AutoCompleteTextView筛选器从“更改为”;用“开始”;至;包含“;?,android,autocompletetextview,Android,Autocompletetextview,我想更改AutoCompleteTextView中的默认筛选。默认筛选查找使用给定标记启动的所有字符串。我的项目要求筛选应该找到包含给定令牌的所有字符串 可能吗 是的,这是可能的 第一种方式: 您应该创建一个实现和的自定义适配器 可以实现“包含”过滤器逻辑的 然后将此适配器设置为AutoCompleteTextView的适配器 第二种方式: 如果您已经在使用。你可以重写它的方法 多亏了谷歌和两天的搜索,我找到了一个解决方案。正如@torque203所建议的,我已经实现了自己的自定义适配器。首先为
AutoCompleteTextView
中的默认筛选。默认筛选查找使用给定标记启动的所有字符串。我的项目要求筛选应该找到包含给定令牌的所有字符串
可能吗 是的,这是可能的
第一种方式:
您应该创建一个实现和的自定义适配器
可以实现“包含”过滤器逻辑的
然后将此适配器设置为AutoCompleteTextView的适配器
第二种方式:
如果您已经在使用。你可以重写它的方法 多亏了谷歌和两天的搜索,我找到了一个解决方案。正如@torque203所建议的,我已经实现了自己的自定义适配器。首先为适配器中的自定义项定义一个新的XML文件: autocomplete_item.xml
名称适配器
公共类名称适配器扩展了ArrayAdapter{
语境;
int资源,textViewResourceId;
列出项目、临时项目、建议;
公共名称适配器(上下文上下文、int资源、int textViewResourceId、列表项){
超级(上下文、资源、textViewResourceId、项目);
this.context=上下文;
这个资源=资源;
this.textViewResourceId=textViewResourceId;
这个项目=项目;
tempItems=newarraylist(items);//这会产生不同。
建议=新建ArrayList();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=转换视图;
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图=充气机。充气(R.layout.autocomplete\u项,父项,false);
}
name=items.get(位置);
如果(名称!=null){
TextView lblName=(TextView)view.findViewById(R.id.lbl_名称);
如果(lblName!=null)
lblName.setText(name.name);
}
返回视图;
}
@凌驾
公共过滤器getFilter(){
返回名称过滤器;
}
/**
*我们提供的自定义建议的自定义筛选器实现。
*/
过滤器名称过滤器=新过滤器(){
@凌驾
public CharSequence ConvertResultString(对象结果值){
字符串str=((名称)resultValue).name;
返回str;
}
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
if(约束!=null){
建议。清晰();
用于(名称:tempItems){
if(names.name.toLowerCase().contains(constraint.toString().toLowerCase())){
建议.增加(姓名);
}
}
FilterResults FilterResults=新的FilterResults();
filterResults.values=建议;
filterResults.count=建议.size();
返回过滤器结果;
}否则{
返回新的FilterResults();
}
}
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
List filterList=(ArrayList)results.values;
if(results!=null&&results.count>0){
清除();
用于(名称:过滤器列表){
增加(姓名);
notifyDataSetChanged();
}
}
}
};
}
SearchActivity(或您的主要活动)
。。。。
List namesList=//您的姓名列表;
名称适配器名称适配器=新名称适配器(
这个,,
R.layout.activity\u搜索,
R.id.lbl\U名称,
名称列表
);
//将适配器设置为listStudent
setAdapter(名称适配器);
autoCompleteTextView.showDropDown();
...
仅对拿铁咖啡的好答案进行扩展:
1) autoCompleteTextView.showDropDown()代码>是不需要的
2) 要检索输入对象,可以使用:
//retrieve the input in the autoCompleteTextView
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//parent The AdapterView where the click happened.
//view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
//position The position of the view in the adapter
//id The row id of the item that was clicked.
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection =parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),"" + selection,Toast.LENGTH_SHORT).show();
}
});
//在autoCompleteTextView中检索输入
autoCompleteTextView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
//设置发生单击的AdapterView的父级。
//查看已单击的AdapterView中的视图(这将是适配器提供的视图)
//在适配器中定位视图的位置
//id单击的项目的行id。
public void onItemClick(AdapterView父级、视图视图、整型位置、长rowId){
字符串选择=parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),“”+selection,Toast.LENGTH_SHORT).show();
}
});
从父对象检索到的对象必须实现toString()方法。//请尝试此操作
autoCompleteText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
YourCustomModel YourFilterdSelectedModel = (YourCustomModel) adapterView.getItemAtPosition(position);
autoCompleteText.setText(str);
}
});
autoCompleteText.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView AdapterView,视图arg1,内部位置,长arg3){
YourCustomModel YourFilterSelectedModel=(YourCustomModel)adapterView.getItemAtPosition(position);
autoCompleteText.setText(str);
}
});
英镑
这对我来说很有效这是@Caffe-Latte发布的一个更简单的Kotlin版本
您不需要自定义布局文件,只需使用默认的android.R.layout.simple\u list\u item\u 1
为此适配器提供任何类,包括普通ol'字符串。它只需使用toString()
来确定显示文本
import android.content.Context
import android.widget.ArrayAdapter
import android.widget.Filter
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import java.util.*
class AutoCompleteAdapter(
context: Context,
@LayoutRes resource: Int,
@IdRes textViewResourceId: Int = 0,
internal var items: List<Any> = listOf()
)
: ArrayAdapter<Any>(context, resource, textViewResourceId, items) {
internal var tempItems: MutableList<Any> = mutableListOf()
internal var suggestions: MutableList<Any> = mutableListOf()
/**
* Custom Filter implementation for custom suggestions we provide.
*/
private var filter: Filter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
return if (constraint != null) {
suggestions.clear()
tempItems.forEach {
if (it.toString().toLowerCase(Locale.getDefault()).contains(constraint.toString().toLowerCase(Locale.getDefault()))) {
suggestions.add(it)
}
}
val filterResults = FilterResults()
filterResults.values = suggestions
filterResults.count = suggestions.size
filterResults
} else {
FilterResults()
}
}
override fun publishResults(constraint: CharSequence?, results: FilterResults) {
val filterList = results.values as? List<Any>
if (results.count > 0) {
clear()
filterList?.forEach {
add(it)
}.also {
notifyDataSetChanged()
}
}
}
}
init {
tempItems = items.toMutableList()
suggestions = ArrayList()
}
override fun getFilter(): Filter {
return filter
}
}
导入android.content.Context
导入android.widget.ArrayAdapter
导入android.widget.Filter
感应电动机
....
List<Names> namesList = //your names list;
NamesAdapter namesAdapter = new NamesAdapter(
SearchActivity.this,
R.layout.activity_search,
R.id.lbl_name,
namesList
);
//set adapter into listStudent
autoCompleteTextView.setAdapter(namesAdapter);
autoCompleteTextView.showDropDown();
...
//retrieve the input in the autoCompleteTextView
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//parent The AdapterView where the click happened.
//view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
//position The position of the view in the adapter
//id The row id of the item that was clicked.
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection =parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),"" + selection,Toast.LENGTH_SHORT).show();
}
});
autoCompleteText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
YourCustomModel YourFilterdSelectedModel = (YourCustomModel) adapterView.getItemAtPosition(position);
autoCompleteText.setText(str);
}
});
import android.content.Context
import android.widget.ArrayAdapter
import android.widget.Filter
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import java.util.*
class AutoCompleteAdapter(
context: Context,
@LayoutRes resource: Int,
@IdRes textViewResourceId: Int = 0,
internal var items: List<Any> = listOf()
)
: ArrayAdapter<Any>(context, resource, textViewResourceId, items) {
internal var tempItems: MutableList<Any> = mutableListOf()
internal var suggestions: MutableList<Any> = mutableListOf()
/**
* Custom Filter implementation for custom suggestions we provide.
*/
private var filter: Filter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
return if (constraint != null) {
suggestions.clear()
tempItems.forEach {
if (it.toString().toLowerCase(Locale.getDefault()).contains(constraint.toString().toLowerCase(Locale.getDefault()))) {
suggestions.add(it)
}
}
val filterResults = FilterResults()
filterResults.values = suggestions
filterResults.count = suggestions.size
filterResults
} else {
FilterResults()
}
}
override fun publishResults(constraint: CharSequence?, results: FilterResults) {
val filterList = results.values as? List<Any>
if (results.count > 0) {
clear()
filterList?.forEach {
add(it)
}.also {
notifyDataSetChanged()
}
}
}
}
init {
tempItems = items.toMutableList()
suggestions = ArrayList()
}
override fun getFilter(): Filter {
return filter
}
}