其实实现ListView过滤功能最方便的便是使用ArrayAdapter,里面自带的getFilter()方法能很方便的实现此功能,但是在实际的开发中,一般都是继承于BaseAdapter。还有一种是利用控件AutoComplete,这种方式只是在输入框的下方重新显示一个列表,显然,很多时候这两种方式也满足不了我们的要求。

在Activity中定义一个类,让它实现TextWatcher接口,然后再onTextChanged方法中去过滤。然后常见相应的Pattern和match,来判断传入的参数时候符合列表中的数据,符合就加入一个新的列表中。

首先贴出实现的效果图

下面贴出主要实现代码

 1 package com.example.demo;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7 import android.os.Bundle;
8 import android.app.Activity;
9 import android.text.Editable;
10 import android.text.TextWatcher;
11 import android.view.Menu;
12 import android.widget.EditText;
13 import android.widget.ListView;
14
15 public class MainActivity extends Activity {
16
17 List<People> people = new ArrayList<People>() ;
18 EditText editinput;
19 ListView listview;
20 Adapter adapter;
21
22
23 @Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.activity_main);
27 editinput = (EditText)findViewById(R.id.edit);
28 listview = (ListView)findViewById(R.id.ydlist);
29 initdata();
30 adapter = new Adapter(getApplicationContext(), people);
31 listview.setAdapter(adapter);
32 editinput.addTextChangedListener(new watcher());
33 }
34
35 void initdata(){
36
37 people.add(new People("张三","1374456"));
38 people.add(new People("张三小子", "12444455"));
39 people.add(new People("李一", "1345555"));
40 people.add(new People("王一", "1355555"));
41 people.add(new People("王二", "1365555"));
42 people.add(new People("李三", "13565555"));
43 people.add(new People("李一", "123555"));
44 }
45
46 class watcher implements TextWatcher{
47
48 @Override
49 public void afterTextChanged(Editable s) {
50 // TODO Auto-generated method stub
51
52 }
53
54 @Override
55 public void beforeTextChanged(CharSequence s, int start, int count,
56 int after) {
57 // TODO Auto-generated method stub
58
59 }
60
61 @Override
62 public void onTextChanged(CharSequence s, int start, int before,
63 int count) {
64 // TODO Auto-generated method stub
65 String aa = s.toString();
66 Pattern p = Pattern.compile(aa);
67 List<People> we = new ArrayList<People>();
68 for(int i=0;i<people.size();i++){
69 People pp = people.get(i);
70 Matcher matcher = p.matcher(pp.getName()+pp.getPhome());
71 if(matcher.find()){
72 we.add(pp);
73 }
74 }
75 adapter = new Adapter(getApplicationContext(), we);
76 listview.setAdapter(adapter);
77 }
78
79 }
80
81 @Override
82 public boolean onCreateOptionsMenu(Menu menu) {
83 // Inflate the menu; this adds items to the action bar if it is present.
84 getMenuInflater().inflate(R.menu.activity_main, menu);
85 return true;
86 }
87
88 }

自定义Adapter

 1 package com.example.demo;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import android.content.Context;
6 import android.view.LayoutInflater;
7 import android.view.View;
8 import android.view.ViewGroup;
9 import android.widget.BaseAdapter;
10 import android.widget.TextView;
11
12 public class Adapter extends BaseAdapter {
13 private List<People> people = new ArrayList<People>();
14 Context ct;
15 private LayoutInflater inflater;
16 public Adapter(Context ct,List<People> people) {
17 // TODO Auto-generated constructor stub
18 this.people = people;
19 this.ct = ct;
20 inflater = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
21 }
22
23 @Override
24 public int getCount() {
25 // TODO Auto-generated method stub
26 return people.size();
27 }
28
29 @Override
30 public Object getItem(int position) {
31 // TODO Auto-generated method stub
32 return people.get(position);
33 }
34
35 @Override
36 public long getItemId(int position) {
37 // TODO Auto-generated method stub
38 return position;
39 }
40
41 @Override
42 public View getView(int position, View convertView, ViewGroup parent) {
43 // TODO Auto-generated method stub
44 People p = people.get(position);
45 if(convertView==null){
46 convertView = inflater.inflate(R.layout.yd_item, null);
47 }
48 TextView tv1=(TextView)convertView.findViewById(R.id.ydtext1);
49 TextView tv2=(TextView)convertView.findViewById(R.id.ydtext2);
50 tv1.setText(p.getName());
51 tv2.setText(p.getPhome());
52 return convertView;
53 }
54
55
56
57 }

实体类

 1 package com.example.demo;
2
3 public class People {
4 private String Name ;
5 private String Phome;
6
7
8
9 public String getName() {
10 return Name;
11 }
12
13
14
15 public void setName(String name) {
16 Name = name;
17 }
18
19
20
21 public String getPhome() {
22 return Phome;
23 }
24
25
26
27 public void setPhome(String phome) {
28 Phome = phome;
29 }
30
31
32
33 public People(String name,String phone){
34 super();
35 this.Name = name;
36 this.Phome = phone;
37 }
38 }

转--Android实现ListView过滤功能,继承于BaseAdapter,非ArrayAdapter。的更多相关文章

  1. Android的ListView分页功能

    一.功能分析----ListView“加载更多”,功能如下图所示: 这个效果是当你上拉拖动页面时(注意有区别于下拉刷新),页面提示正在加载,2秒后显示留言更多内容:具体功能知道那我就来讲解下如何实现这 ...

  2. Android的ListView分页功能(上滑加载更多)

    今天主要工作是将之前实现的各种ListView显示全部信息,优化成了每次加载几条数据,然后上滑的时候加载更多,底部显示一个进度条和一个文字提示,然后加载完毕后,将提示信息隐藏. 一边看教学视频一遍敲代 ...

  3. Android学习---ListView的点击事件,simpleAdapter和arrayadapter,SimpleCursoAdapter的原理和使用

    如题,本文将介绍 listview的点击事件,simpleAdapter和arrayadapter的原理和使用. 1.ListView的注册点击事件 //注册点击事件 personListView.s ...

  4. Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音

    转载:http://blog.csdn.net/xiaanming/article/details/12684155 转载请注明出处:http://blog.csdn.net/xiaanming/ar ...

  5. Android 使用ListView的A-Z字母排序功能实现联系人模块

    在上一篇文章其中,主要学习了ListView的A-Z字母排序功能以及依据输入框的输入值改变来过滤搜索结果,假设输入框里面的值为空.更新为原来的列表,否则为过滤数据列表,包含汉字转成拼音的功能.假设你还 ...

  6. 分享个刚写好的 android 的 ListView 动态加载类,功能全而代码少。

    (转载声明出处:http://www.cnblogs.com/linguanh/) 简介:      该ListView 实现动态加载数据,为了方便用户充分地自定义自己的数据源.点击事件,等核心操作, ...

  7. 含有过滤功能的android流式布局

    FilterFlowLayout 含有过滤功能的流式布局, 參考FlowLayout 能够去除宽度不在范围(比例或真实值)内的子view 能够设置最大行数 能够加入组件间水平间距 能够加入行间距 系统 ...

  8. 【Android】Listview返回顶部,快速返回顶部的功能实现,详解代码。

    作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 首先给大家看一下我们今天这个最终实现 ...

  9. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

随机推荐

  1. 【P1326】超级教主

    DP优化 原题: LHX教主很能跳,因为Orz他的人太多了.教主跳需要消耗能量,每跳1米就会消耗1点能量,如果教主有很多能量就能跳很高.教主为了收集能量,来到了一个神秘的地方,这个地方凡人是进不来的. ...

  2. 三星Mega 6.3(i9200)删除kingroot

    关于kingroot实际效果怎么样,是不是流氓软件,我不做评论. 手机型号,三星galaxy mega 6.3(i9200)水货,更详细的机型不知道怎么看. 刷了官方的rom以后,下载了kingroo ...

  3. MMU讲解

    MMU是Memory Management Unit的缩写,中文名是内存管理单元,它是中央处理器(CPU)中用来管理虚拟存储器.物理存储器的控制线路,同时也负责虚拟地址映射为物理地址,以及提供硬件机制 ...

  4. StackTrace,Trim

    一: Environment.StackTrace 可能我们看到最多的就是catch中的e参数,里面会有一个StackTrace,然后不可否认的这玩意太有用了,它会把调用堆栈 中的信息输出出来,有了它 ...

  5. Python3下map函数的显示问题

    map函数是Python里面比较重要的函数,设计灵感来自于函数式编程.Python官方文档中是这样解释map函数的: map(function, iterable, ...) Return an it ...

  6. Linux文件普通权限

    1.文件所有者,所属用户组,其他用户1)文件所有者:创建文件的用户2)所属用户组:文件属于哪个用户组3)其他用户:不是文件所有者,不属于文件所属用户组的用户,称为其他用户 2.Linux文件权限我们切 ...

  7. maxscript,#号和$号

    一,#号: 1,数组 #(123, "hi") 2,系统目录 fileName = getDir #scripts + "\\xxx.ms" 二,$号 节点路径 ...

  8. 火狐和ie下获取javascript 获取event

    javascript 获取event 先从一个简单的例子说起,一个简单的button控件如下: <input type='button' name='mybtn' id='mybtn' oncl ...

  9. js事件源window.event.srcElement兼容性写法

    <html> <body> <p>一个好处就是 我想让body(或其他元素内)的某些对象响应事件 就不用挨个儿去写 只要在外层上写一个 然后检查event.srcE ...

  10. 设置页面不缓存 no-cache

    html中设置方法 <head> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <M ...