gpt4 book ai didi

Android SearchView 过滤 ListView

转载 作者:IT老高 更新时间:2023-10-28 23:11:29 38 4
gpt4 key购买 nike

我已经在我的 SherlockAction Bar 中为我的 SearchView 实现了搜索过滤器。

当我输入 m 时,我想在下面的 ListView 中显示过滤结果,该 ListView 仅以 M 开头,依此类推。但现在它显示的是随机结果。

enter image description here enter image description here

public boolean onQueryTextChange(String newText) {
Log.i("Nomad", "onQueryTextChange");

if (TextUtils.isEmpty(newText)) {
adapter.getFilter().filter("");
Log.i("Nomad", "onQueryTextChange Empty String");
placesListView.clearTextFilter();
} else {
Log.i("Nomad", "onQueryTextChange " + newText.toString());
adapter.getFilter().filter(newText.toString());
}
return true;
}

public boolean onQueryTextSubmit(String query) {
Log.i("Nomad", "onQueryTextSubmit");
return false;
}

public boolean onClose() {
Log.i("Nomad", "onClose");
return false;
}

最佳答案

把它放在你的适配器里面:

@Override
public Filter getFilter(){
return new Filter(){

@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();

if (constraint != null && constraint.toString().length() > 0) {
List<String> founded = new ArrayList<String>();
for(YourListItemType item: origData){
if(item.toString().toLowerCase().contains(constraint)){
founded.add(item);
}
}

result.values = founded;
result.count = founded.size();
}else {
result.values = origData;
result.count = origData.size();
}
return result;


}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
for (String item : (List<String>) results.values) {
add(item);
}
notifyDataSetChanged();

}

}
}

你的适配器的内部构造函数

public MyAdapter(Context context, int layoutResourceId, String[] places) {
super(context, layoutResourceId, data);
this.context = context;

this.data = Arrays.asList(places);
this.origData = new ArrayList<String>(this.data);

}

关于Android SearchView 过滤 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14038331/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com