Filter 过滤器

Filter(过滤器) 过滤器的基本概念Servlet 过滤器从字面可理解为经过一层层的过滤处理才达到使用的要求,而其实 Servlet 过滤器就是服务器与客户端请求与响应的中间层组件。在实际项目开发中 Servlet 过滤器主要用于对浏览器的请求进行过滤处理,将过滤后的请求再转给下一个资源。FilterFilter(过滤器)是 JavaWeb 三大组件之一(另外两个是 Servlet 和 Listener),是在 2000 年发布的 Servlet2.3 规范中加入的一个接口,是 Servlet 规范中非常实用的技术。 阅读详情

    一、结构

    public abstract class Filter extends Object
 
java.lang.Object
     android.widget.Filter
 

  二、概述

    过滤器通过过滤模式来约束数据,通常由实现了Filterable接口的子类来生成。 过滤操作是通过调用 filter(CharSequence) 或者 filter(CharSequence, android.widget.Filter.FilterListener)这些异步方法来完成的。以上方法一旦被调用,过滤请求就会被递交到请求队列中等待处理,同时该操作会取消那些之前递交的但是还没有被处理的请求。
 

  三、构造函数

         public Filter ()
  创建一个新的异步过滤器。
 
 

  四、公共方法

 
  public CharSequence convertResultToString (Object resultValue)
        将受过滤的集合对象转换成CharSequence文本。所有继承了Filter的子类应该重写该方法。该方法的默认实现:如果参数为null则返回空字符串或者返回参数的字符串形式.
  参数
                            resultValue 转换成CharSequence文本的对象
                 返回值
                            CharSequence 文本
                  
  public final void filter(CharSequence constraint, Filter.FilterListener listener)
  启动一个异步的过滤操作。对该方法的调用会取消之前队列中等待处理的过滤请求并且递交新的过滤请求等待执行。完成过滤操作之后,通知监听器。
  参数
                   constraint 过滤数据的约束条件
  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)
                        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的用户)
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>
      user.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingTop="1dip"
    android:paddingBottom="1dip"
    >
<TextView android:id="@+id/name"
    android:layout_width="150dip"
    android:layout_height="30dip"
    android:textSize="12pt"
    />
<TextView android:id="@+id/age"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12pt"
    />
</LinearLayout>


码简单的说就是ListView上面有一个SearchBox,然后searchbox里输入内容后对下面listview进行过滤。
涉及的控件:ListView必须有,EditText用来自定义SearchBox
大概就是这样:
         
先看这个有图片的EditText,实现方法有两个,一是用相对布局RelativeLayout + ImageView + EditText。
二是直接用EditText的一个属性DrawableLeft,简单的UI这个就可以实现了
所以这个Activity的布局就很简单,可以用ListActivity实现:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  
    <EditText  
        android:id="@+id/searchbox"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:drawableLeft="@drawable/searchbox"  
        android:hint="Search"  
        android:drawablePadding="5dp"  
        android:singleLine="true"  
        android:ems="10" >  
        <requestFocus />  
    </EditText>  
  
      
    <ListView   
        android:id="@android:id/list"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_below="@id/searchbox">  
    </ListView>  
   
</RelativeLayout>  

再说过滤功能:这个感觉不想搜索,就像是简单的过滤,如果涉及到去数据库取数据那个才是搜索了
用到了Filterable接口,Filter类

要让数据有过滤功能,我们需要在继承的BaseAdapter的基础上再实现Filterable接口的getFilter方法,同时在Adapter内部写一个继承Filter的内部类来完成过滤功能:

private class ListAdapter extends BaseAdapter implements Filterable {  
  
        private List<Person> list;  
          
        private Context context;  
          
        private PersonFilter filter;  
          
        public ListAdapter(List<Person> list, Context context) {  
            this.list = list;  
            this.context = context;  
        }  
  
        @Override  
        public int getCount() {  
            return list.size();  
        }  
  
        @Override  
        public Object getItem(int position) {  
            return list.get(position);  
        }  
  
        @Override  
        public long getItemId(int position) {  
            return position;  
        }  
  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            if (convertView == null) {  
                convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);  
            }  
            Person p = list.get(position);  
            TextView firstname = (TextView)convertView.findViewById(R.id.firstname);  
            TextView lastname = (TextView)convertView.findViewById(R.id.lastname);  
            TextView age = (TextView)convertView.findViewById(R.id.age);  
              
            firstname.setText(p.firstname);  
            lastname.setText(p.lastname);  
            age.setText(p.age + "");  
            return convertView;  
        }  
  
        @Override  
        public Filter getFilter() {  
            if (filter == null) {  
                filter = new PersonFilter(list);  
            }  
            return filter;  
        }  
          
        private class PersonFilter extends Filter {  
              
            private List<Person> original;  
              
            public PersonFilter(List<Person> list) {  
                this.original = list;  
            }  
  
            @Override  
            protected FilterResults performFiltering(CharSequence constraint) {  
                FilterResults results = new FilterResults();  
                if (constraint == null || constraint.length() == 0) {  
                    results.values = original;  
                    results.count = original.size();  
                } else {  
                    List<Person> mList = new ArrayList<Person>();  
                    for (Person p: original) {  
                        if (p.firstname.toUpperCase().startsWith(constraint.toString().toUpperCase())  
                            || p.lastname.toUpperCase().startsWith(constraint.toString().toUpperCase())  
                            || new String(p.age + "").toUpperCase().startsWith(constraint.toString().toUpperCase())) {  
                            mList.add(p);  
                        }  
                    }  
                    results.values = mList;  
                    results.count = mList.size();  
                }  
                return results;  
            }  
  
            @Override  
            protected void publishResults(CharSequence constraint,  
                    FilterResults results) {  
                list = (List<Person>)results.values;  
                notifyDataSetChanged();  
            }  
              
        }  
    }  
Filter类中的两个方法看名字就是知道一个是执行过滤的,一个刷新listview数据展现结果的。其中我采用了前缀匹配,就是用输入的字符串和ListView里的所有Person的各个属性的前缀做比较。也可以用更加复杂的匹配,例如正则表达式。
以上关键代码。非关键代码是Person类以及List_Item的布局:

private class Person {  
        public String firstname;  
        public String lastname;  
        public int age;  
          
        public Person(String firtname, String lastname, int age) {  
            this.firstname = firtname;  
            this.lastname = lastname;  
            this.age = age;  
        }  
    }  

<?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="horizontal" >  
  
    <TextView  
        android:id="@+id/firstname"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_margin="10dp"  
        android:text="Firstname" />  
  
    <TextView  
        android:id="@+id/lastname"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_margin="10dp"  
        android:text="Lastname" />  
      
    <TextView  
        android:id="@+id/age"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_margin="10dp"  
        android:text="Age" />  
  
</LinearLayout>  



欢迎扫描二维码,关注公众号


JavaWeb过滤器(Filter)详解 Filter表示过滤器,是 JavaWeb三大组件(Servlet、Filter、Listener)之一。过滤器可以把对资源的请求拦截下来,从而实现一些特殊的功能过滤器一般完成一些通用的操作,比如:登录校验、统一编码处理、敏感字符处理等。下面我们通过Filter快速入门程序掌握过滤器的基本使用操作:第1步,定义过滤器 :1.定义一个类,实现 Filter 接口,并重写其所有方法。第2步,配置过滤器:Filter类上加 @WebFilter 注解,配置拦截资源的路径。 阅读详情

相关推荐

Windows系统下搭建MPI(并行计算)环境

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;W

weixin_43417960的博客 5753

.NetCore3 Swagger 使用 SchemaFilter 给你 API Swagger 自定义请求参数显示

原理 通过 Attribute来获取特别的参数,需要忽略或者增加的都可以,然后再实现 ISchemaFilter,通过反射获取到这些Attributes,最后根据获取到的属性来给 OpenApiSchema删除或者添加属性 IOperationFilter 来源于命名空间:namespace Swashbuckle.AspNetCore.SwaggerGen 官方应用源码 具体代码 属性定义 首先...

SmileSB101-(ZXB)的博客 4751

【CANoe16安装教程】亲测安装成功

CANoe16安装教程

qq_45302638的博客 2万+

.NET 使用 ISchemaFilter 过滤器修改参数默认值

一、 背景 [HttpPost] public string Test(SearchReq search) { return "ok" + search.Name; } 从 swagger 中可以看出, 字符串若为空,则默认提交string,若要修改为指定字符,可修改过滤器 二、修改策略 2.1 创建过滤器 通过类型设置默认值 ``` using Microsoft.Ope...

xy的博客 459

开关变换器小信号建模实战:从理论到环路设计

本文深入探讨了开关变换器小信号建模的实战技巧,从理论基础到环路设计全面解析。通过Buck和Boost变换器的具体案例,详细介绍了状态空间平均法、扰动法等关键建模技术,并分享环路补偿设计和实际调试中的宝贵经验。文章特别强调了小信号建模在电源设计中的重要性,为工程师提供了从理论到实践的完整解决方案。

weixin_29290259的博客 382

matlab 三维绘图 抛光,瓷砖抛光过程建模与仿真

:瓷砖抛 光过程 建模与仿真朱 成 就(广 东工业大学 机 电工程 学院, 广东广州 )摘要 :瓷砖的抛磨影响 因素众 多 ,实际磨削加工时容易出现漏抛 、返抛现象 。对陶瓷抛光机磨头进行运动学分析 ,并基于不 同粒度号 的磨 粒在一定压力下的加工深度模型 ,探讨抛光机磨 头转 速 、横梁摆动频率 、传送速度对瓷砖抛光加工表 面均匀性 的影响,然 后结合 磨削机理使 用 对陶瓷抛光过程进行仿真 ...

weixin_36331058的博客 754

Asp.Net Core中使用Swagger,你不得不踩的坑

.NetCore3 Swagger 使用 SchemaFilter 给你 API Swagger 自定义请求参数显示 NeverSettle101 2020-01-22 16:17:31 2104 收藏 分类专栏: .NetCore3+ 自定义属性 版权 .NetCore3+ 同时被 2 个专栏收录 8 篇文章0 订阅 订阅专栏 自定义属性 2 篇文章0 订阅 订阅专栏 原理 通过 Attribute来获取特别的参数,需要忽略或者增加的都可以,然后再实现 ISchemaFilter,通过反射获取到..

追随内心 1885

Fluent新手必看:如何正确解读scaled residuals曲线(附常见问题排查)

本文详细解析了Fluent中scaled residuals曲线的解读方法及常见问题排查技巧。针对continuity残差居高不下、湍流模型收敛困难等典型问题,提供了网格质量检查、边界条件验证和求解策略调整的实用解决方案,帮助CFD新手快速掌握计算收敛性的关键判断标准。

weixin_28698059的博客 380

ASP.NETCore Swagger/OpenAPI 反序列化失败:属性不允许为空

解决ASP.NETCore开发中使用Swagger/OpenAPI时,客户端请求遇到反序列化失败(属性不允许为空)的问题。

猿长大人 1078

详解YOLOv5网络结构/数据集获取/环境搭建/训练/推理/验证/导出/部署

本文给大家带来的教程是利用YOLOv5训练自己的数据集,以及有关YOLOv5的网络结构讲解/数据集获取/环境搭建/训练/推理/验证/导出/部署相关的教程,同时通过示例的方式让大家来了解具体的操作流程,过程中还分享给大家一些好用的资源,我还对其中的参数进行了详细的讲解,通过本文大家可以成功训练自己的数据集,在开始之前给大家推荐一下我的YOLOv5专栏,本专栏包含上百种YOLOv5的改进机制以及如何撰写论文的教程,手把手教大家如何改进YOLOv5非常适合科研小白。

Snu77的博客 4912

qt实现OCC读取STEP模型并显示(附完整源码)

qt实现OCC读取STEP模型并显示(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1493

InputFilter方法filter 解释

public interface InputFilter android.text.InputFilter Known Indirect Subclasses DateKeyListener, DateTimeKeyListener, DialerKeyListener, DigitsKeyListener, Input

y394996630的专栏 3579

Android插件化和组件化面试题及参考答案

如何使用 Gradle 来管理 Android 项目中的模块依赖? Gradle 是一个强大的构建工具,在 Android 项目中管理模块依赖主要通过以下方式。 首先,在项目的根目录下的 build.gradle 文件中,需要配置仓库。一般会添加如 Google 的 Maven 仓库和 JCenter 等,例如在 repositories 部分添加: repositories { google() jcenter() } 对于模块依赖,在每个模块(例如 app

大模型大数据攻城狮的专栏 1189

Filter过滤器

过滤器(Filter)用于根据预定义的条件对输入数据进行筛选,只保留满足条件的数据项。这个过程类似于在现实生活中使用筛子筛选谷物,去除杂质,留下纯净的部分。在计算机科学中,过滤器通常接收一个数据集作为输入,并返回一个新的数据集,其中包含原始数据集中符合特定标准的元素。是 JavaWeb三大组件(Servlet、Filter、Listener)之一。

2302_77758423的博客 2385

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

过滤器Filter

1.过滤器是什么? 过滤器是Servlet的高级特性之一,也别把它想得那么高深,只不过是实现Filter接口的Java类。 1.过滤器是什么?

m0_61609890的博客 3207

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Java中的过滤器Filter

一:Filter简介 二:关于Filter (一)如何使用Filter进行过滤 (二)过滤器链 (FilterChain) (三)Filter的配置 1.通过web.xml文件配置 2.通过注解配置 (四)Filter的生命周期 (五)中文乱码过滤器 三:测试案例 1.单个过滤器 2.多个过滤器

m0_68988603的博客 1万+

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Filter过滤器教程

Filter过滤器教程-IDEA

zhulir的博客 1925

Filter 过滤器的介绍及使用

Filter过滤器 MySql的语法看这篇就够了,传送地址:MySql必知必会 JSP的使用看这篇就够了,传送地址:JSP必知必会 文章目录Filter过滤器一、Filter过滤器的介绍二、Filter过滤器的使用三、Filter过滤器的生命周期四、FilterConfig类五、FilterChain过滤器链六、Filter的拦截路径 一、Filter过滤器的介绍 Filter过滤器是JavaWeb三大组件之一,其余组件是:Servlet程序、Listener监听器 Filter过滤器是JavaEE规范,

立志成为一个前端、后端、测试全方位发展的程序员 6871

Filter过滤器讲解

Filter过滤器的基本原理和基本用法

徐大大的博客 2069

Filter的过滤器链

Filter的介绍 过滤器链 我们已经了解了过滤器,并且知道可以配多个过滤器,那么多个滤器之间的执行顺序是什么样的呢 我们建立8个过滤器,demo1,demo2,demo3,和demo11在web.xml配置,demo4,demo5,demo6,和demo41用注解配置 demo1代码 package com.lingaolu.filter; import javax.servlet.*; import java.io.IOException; /** * @aut.

林高禄 6874

Filter过滤器是什么?

1.what filter过滤器的作用:拦截请求 2.why 网页上的某些操作或数据只有用户登录之后才能访问,用filter来过滤掉未登录的用户,只让已经登录的用户继续访问 3.for 利于数据的管理 4.code public class ManagerFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { }

weixin_44853600的博客 1820
上一篇: Android - LayoutInflater和inflate方法的用法
下一篇: JNI签名与数据匹配
robert_chao
博客等级 码龄16年 265粉丝 119原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址