Android数据过滤器:Filter
类图:
通常可以将SearchView和ListView结合,实现数据的搜索和过滤。
1.监听SearchView,SearchView.setOnQueryTextListener(OnQueryTextListener listener);
2.开启ListView的过滤功能,listView.setTextFilterEnabled(true)。必须开启,否则不会过滤;
3..当SearchView接收到输入事件后,调用ListView.setFilterText(filterText)方法,该方法会通过Adapter得到Filter,然后调用Filter.filter(filterText):
<span style="font-size:14px;"> public void setFilterText(String filterText) {
// TODO: Should we check for acceptFilter()?
if (mTextFilterEnabled && !TextUtils.isEmpty(filterText)) {
createTextFilter(false);
// This is going to call our listener onTextChanged, but we might not
// be ready to bring up a window yet
mTextFilter.setText(filterText);
mTextFilter.setSelection(filterText.length());
if (mAdapter instanceof Filterable) {
// if mPopup is non-null, then onTextChanged will do the filtering
if (mPopup == null) {
Filter f = ((Filterable) mAdapter).getFilter();
f.filter(filterText);
}
// Set filtered to true so we will display the filter window when our main
// window is ready
mFiltered = true;
mDataSetObserver.clearSavedState();
}
}
} </span>
4.Filter.filter(filterText)方法最终会调用Filter.performFiltering(filterText)和Filter.publishResults(CharSequence filterText, FilterResults results)。performFiltering(filterText)方法完成过滤处理并且返回结果FilterResults,而publishResults(CharSequence
filterText, FilterResults results)则根据返回的结果进行相应的处理。
5.Filter.publishResults(CharSequence filterText, FilterResults results)调用了BaseAdapter.notifyDataSetChanged()方法,该方法用于当Adapter的数据发生变化时,通知UI主线程根据新的数据绘制界面:
<span style="font-size:14px;"> @Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//noinspection unchecked
mObjects = (List<T>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
} </span>
数据过滤就这样完成了。
下面给出例子。
布局文件filter_activity.xml:
<span style="font-size:14px;"> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <SearchView
android:id="@+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</SearchView> <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout> </span>
类文件MainActivity.java:
<span style="font-size:14px;"> package com.zzj.ui.filterdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import com.zzj.ui.R;
public class MainActivity extends Activity implements OnQueryTextListener {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filter_activity);
SearchView searchView = (SearchView) findViewById(R.id.searchView1);
searchView.setOnQueryTextListener(this);
searchView.setSubmitButtonEnabled(false);
searchView.setIconifiedByDefault(false);
listView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, new String[] { "Bei jing",
"Shang hai", "Chang sha", "Chang chun", "Nan jing",
"Dong jing", "Ji nan", "Qing dao", "Xiang tan",
"Zhu zhou", "Heng yang" });
listView.setAdapter(adapter);
// 开启过滤功能
listView.setTextFilterEnabled(true);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText == null || newText.length() == 0) {
listView.clearTextFilter();
} else {
listView.setFilterText(newText);
}
return true;
}
} </span>
效果图:
如图所示,弹出了一个浮动框,这是listView.setFilterText(filterText)弹出来的。如果不想要这个浮动框,可以先获取Filter,然后调用Filter.filter(filterText)。
修改SearchView的监听函数如下:
@Override
public boolean onQueryTextChange(String newText) {
ListAdapter adapter = listView.getAdapter();
if (adapter instanceof Filterable) {
Filter filter = ((Filterable) adapter).getFilter();
if (newText == null || newText.length() == 0) {
filter.filter(null);
} else {
filter.filter(newText);
}
}
return true;
}
使用这种方法不需要开启ListView的过滤功能。效果如下:
上面使用的是ArrayAdapter的过滤功能,我们也可以继承BaseAdapter,然后实现Filterable接口,定义自己的过滤器。
from:http://blog.csdn.net/zhangzeyuaaa/article/details/40187789
Android数据过滤器:Filter的更多相关文章
- Android 数据过滤器:Filter
类图: 通常可以将SearchView和ListView结合,实现数据的搜索和过滤. 1.监听SearchView,SearchView.setOnQueryTextListener(OnQueryT ...
- ABP(现代ASP.NET样板开发框架)系列之13、ABP领域层——数据过滤器(Data filters)
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之13.ABP领域层——数据过滤器(Data filters) ABP是“ASP.NET Boilerplate P ...
- ABP理论学习之数据过滤器
返回总目录 本篇目录 介绍 预定义过滤器 关闭过滤器 开启过滤器 设置过滤器参数 定义自定义过滤器 其他ORM 介绍 软删除模式通常用于不会真正从数据库删除一个实体而是仅仅将它标记为"已删除 ...
- Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例
Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...
- java Servlet中的过滤器Filter
web.xml中元素执行的顺序listener->filter->struts拦截器->servlet. 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...
- Servlet中的过滤器Filter用法
1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对Http ...
- Servlet中的过滤器Filter详解
加载执行顺序 context-param->listener->filter->servlet web.xml中元素执行的顺序listener->filter->stru ...
- AngularJS的过滤器$filter
过滤器(filter)主要用于数据的格式上,通过某个规则,把值处理后返回结果.例如获得数据集,可排序后再返回. ng内置的共有九种过滤器: currency 货币 使用currency可以将数字格式化 ...
- ASP.NET没有魔法——ASP.NET MVC 过滤器(Filter)
上一篇文章介绍了使用Authorize特性实现了ASP.NET MVC中针对Controller或者Action的授权功能,实际上这个特性是MVC功能的一部分,被称为过滤器(Filter),它是一种面 ...
随机推荐
- Hadoop第一课:Hadoop集群环境搭建
一. 检查列表 1.1.网络访问 设置电脑IP以及可以访问网络设置:进入etc/sysconfig/network-scripts/,使用命令“ls -all” 查看文件.会看到ifcfg-lo文件然 ...
- solidity事件详解
很多同学对Solidity 中的Event有疑问,这篇文章就来详细的看看Solidity 中Event到底有什么用? 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊. ...
- NMAP-端口扫描
1.时序选项 -T0 -> -T5 速度变快,但是准确性下降,nmap默认是T3 2.指定端口 3.扫描指定TCP和UDP端口 4.快速扫描常见100个端口 5.扫描常见的n的端口 6.TCP ...
- HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)
Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...
- Thunder团队第六周 - Scrum会议3
Scrum会议3 小组名称:Thunder 项目名称:i阅app Scrum Master:李传康 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...
- 计算器软件实现系列(五)策略模式+asp.net
一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...
- ACM 第十六天
计算几何 练习题: F - Beauty Contest POJ - 2187 Bessie, Farmer John's prize cow, has just won first place in ...
- Linux内核策略介绍学习笔记
主要内容 硬件 策略 CPU 进程调度.系统调用.中断 内存 内存管理 外存 文件IO 网络 协议栈 其他 时间管理 进程调度 内核的运行时间 系统启动.中断发生.系统调用以及内核线程. 进程和线程的 ...
- 第26天:js-$id函数、焦点事件
一.函数return语句定义函数的返回值,在函数内部用return来设置返回值,一个函数只能有一个返回值.同时,终止代码的执行.所有自定义函数默认没有返回值return后面不要换行 var a=10, ...
- Lucene笔记二
lucene 的排序 package cn.itcast.lucene; import java.io.IOException; import org.apache.lucene.document.D ...