listener 监听过滤操作完成之后发出的通知
参见
filter(CharSequence)
performFiltering(CharSequence)
publishResults(CharSequence, android.widget.Filter.FilterResults)
public final void filter(CharSequence constraint)
启动一个异步的过滤操作。对该方法的调用会取消之前队列中等待处理的过滤请求并且递交新的过滤请求等待执行。
参数
constraint 过滤数据的约束条件
参见
filter(CharSequence, android.widget.Filter.FilterListener)
五、 受保护方法
protected abstract Filter.FilterResults performFiltering (CharSequence constraint)
根据约束条件调用一个工作线程过滤数据。子类必须实现该方法来执行过滤操作。过滤结果以Filter.FilterResults的形式返回,然后在UI线程中通过publishResults(CharSequence,android.widget.Filter.FilterResults)方法来发布。
约定:当约束条件为null时,原始数据必须被恢复。
参数
constraint 约束条件
返回值
过滤结果
参见
filter(CharSequence, android.widget.Filter.FilterListener)
publishResults(CharSequence, android.widget.Filter.FilterResults)
protected abstract void publishResults (CharSequence constraint, Filter.FilterResults results)
通过调用UI线程在用户界面发布过滤结果。子类必须实现该方法来显示performFiltering(CharSequence)的过滤结果。
参数
constraint 约束条件
results 过滤结果
参见
filter(CharSequence, android.widget.Filter.FilterListener)
performFiltering(CharSequence)
Filter.FilterResults
六、补充
示例代码
未过滤的数据如下图所示:
为过滤器设置约束条件(只显示年龄为22的用户):
![](http://ima
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
ges.cnblogs.com/cnblogs_com/over140/2010/11/2010-11-25_2.jpg)
TestFilter.java
public class TestFilter extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList < HashMap < String,String >> list = new ArrayList < HashMap < String, String >> ();
HashMap < String, String > map1 = new HashMap < String, String > ();
HashMap < String, String > map2 = new HashMap < String, String > ();
HashMap < String, String > map3 = new HashMap < String, String > ();
map1.put( " name " , " henly " );
map1.put( " age " , " 22 " );
map2.put( " name " , " john " );
map2.put( " age " , " 23 " );
map3.put( " name " , " lilei " );
map3.put( " age " , " 22 " );
list.add(map1);
list.add(map2);
list.add(map3);
SimpleAdapter simpleAdapter = new SimpleAdapter( this , list, R.layout.user, new String[]{ " name " , " age " }, new int []{R.id.name,R.id.age});
String str = new String( " 22 " );
CharSequence constraint = str.subSequence( 0 , str.length());
Filter filter = simpleAdapter.getFilter(); // 得到一个过滤器
filter.filter(constraint); // 为该过滤器设置约束条件
setListAdapter(simpleAdapter);
}
}
main.xml
<? xml version="1.0" encoding="utf-8" ?>< LinearLayout xmlns:android =“http://schemas.android.com/apk/res/android” android:orientation =“vertical” android:layout_width =“fill_parent” android:layout_height =“fill_parent” > < LinearLayout android:id ="@+id/listlinearlayout" android:layout_width =“fill_parent” android:layout_height =“wrap_content” android:orientation =“vertical” > < ListView android:id ="@id/android:list" android:layout_width =“fill_parent” android:layout_height =“wrap_content” android:drawSelectorOnTop =“false” android:scrollbars =“vertical” /> </ LinearLayout >
</ LinearLayout >
d:layout_width =“fill_parent” android:layout_height =“wrap_content” android:drawSelectorOnTop =“false” android:scrollbars =“vertical” /> </ LinearLayout >
</ LinearLayout >