Android的搜索查看过滤器的ListView [英] Android SearchView Filter ListView
本文介绍了Android的搜索查看过滤器的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在SherlockAction酒吧实现搜索过滤器,我的搜索查看。
当我键入英里想显示筛选结果列表视图下方只以M开头等。但现在它显示随机的结果。
公共布尔onQueryTextChange(字符串newText){
Log.i(游牧,onQueryTextChange);
如果(TextUtils.isEmpty(newText)){
。adapter.getFilter()过滤();
Log.i(保荐人,onQueryTextChange空字符串);
placesListView.clearTextFilter();
} 其他 {
Log.i(游牧,onQueryTextChange+ newText.toString());
。adapter.getFilter()滤波器(newText.toString());
}
返回true;
}
公共布尔onQueryTextSubmit(查询字符串){
Log.i(游牧,onQueryTextSubmit);
返回false;
}
公共布尔的OnClose(){
Log.i(保荐人,的OnClose);
返回false;
}
解决方案
将这种适配器里面:
@覆盖
公共过滤用getFilter(){
返回新的过滤器(){
@覆盖
保护FilterResults performFiltering(CharSequence的约束){
约束= constraint.toString()与toLowerCase()。
FilterResults导致=新FilterResults();
如果(约束= NULL和放大器;!&安培; constraint.toString()长度()0){
名单<字符串>公司成立=新的ArrayList<字符串>();
对于(YourListItemType项目:origData){
如果(item.toString()。与toLowerCase()。包括(约束)){
founded.add(项目);
}
}
result.values =成立;
result.count = founded.size();
}其他 {
result.values = origData;
result.count = origData.size();
}
返回结果;
}
@覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果){
明确();
对于(字符串项:(名单<字符串>)results.values){
加(项目);
}
notifyDataSetChanged();
}
}
}
和适配器的这里面的构造
公共MyAdapter(上下文的背景下,INT layoutResourceId,字符串[]的地方){
超级(上下文,layoutResourceId,数据);
this.context =背景;
this.data = Arrays.asList(地方);
this.origData =新的ArrayList<字符串>(this.data);
}
I have implemented Search Filter to my SearchView in my SherlockAction Bar.
When i type m i want to show filtered results in the list view below which only starts with M and so on. But now it shows random results.
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;
}
解决方案
Place this inside your adapter:
@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();
}
}
}
And this inside constructor of your adapter
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的搜索查看过滤器的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文