其实实现ListView过滤功能最方便的便是使用ArrayAdapter,里面自带的getFilter()方法能很方便的实现此功能,但是在实际的开发中,一般都是继承于BaseAdapter。还有一种是利用控件AutoComplete,这种方式只是在输入框的下方重新显示一个列表,显然,很多时候这两种方式也满足不了我们的要求。
在Activity中定义一个类,让它实现TextWatcher接口,然后再onTextChanged方法中去过滤。然后常见相应的Pattern和match,来判断传入的参数时候符合列表中的数据,符合就加入一个新的列表中。
首先贴出实现的效果图
下面贴出主要实现代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package
com.example.demo; import
java.util.ArrayList; import
java.util.List; import
java.util.regex.Matcher; import
java.util.regex.Pattern; import
android.os.Bundle; import
android.app.Activity; import
android.text.Editable; import
android.text.TextWatcher; import
android.view.Menu; import
android.widget.EditText; import
android.widget.ListView; public
class
MainActivity extends Activity { List<People>
people = new
ArrayList<People>() ; EditText
editinput; ListView
listview; Adapter
adapter; @Override protected
void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editinput
= (EditText)findViewById(R.id.edit); listview
= (ListView)findViewById(R.id.ydlist); initdata(); adapter
= new
Adapter(getApplicationContext(), people); listview.setAdapter(adapter); editinput.addTextChangedListener( new
watcher()); } void
initdata(){ people.add( new
People( "张三" , "1374456" )); people.add( new
People( "张三小子" ,
"12444455" )); people.add( new
People( "李一" ,
"1345555" )); people.add( new
People( "王一" ,
"1355555" )); people.add( new
People( "王二" ,
"1365555" )); people.add( new
People( "李三" ,
"13565555" )); people.add( new
People( "李一" ,
"123555" )); } class
watcher implements TextWatcher{ @Override public
void
afterTextChanged(Editable s) { //
TODO Auto-generated method stub } @Override public
void
beforeTextChanged(CharSequence s, int
start, int
count, int
after) { //
TODO Auto-generated method stub } @Override public
void
onTextChanged(CharSequence s, int
start, int
before, int
count) { //
TODO Auto-generated method stub String
aa = s.toString(); Pattern
p = Pattern.compile(aa); List<People>
we = new
ArrayList<People>(); for ( int
i=0;i<people.size();i++){ People
pp = people. get (i); Matcher
matcher = p.matcher(pp.getName()+pp.getPhome()); if (matcher.find()){ we.add(pp); } } adapter
= new
Adapter(getApplicationContext(), we); listview.setAdapter(adapter); } } @Override public
boolean onCreateOptionsMenu(Menu menu) { //
Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main,
menu); return
true ; } } |
自定义Adapter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package
com.example.demo; import
java.util.ArrayList; import
java.util.List; import
android.content.Context; import
android.view.LayoutInflater; import
android.view.View; import
android.view.ViewGroup; import
android.widget.BaseAdapter; import
android.widget.TextView; public
class
Adapter extends
BaseAdapter { private
List<People> people = new
ArrayList<People>(); Context
ct; private
LayoutInflater inflater; public
Adapter(Context ct,List<People> people) { //
TODO Auto-generated constructor stub this .people
= people; this .ct
= ct; inflater
= (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public
int
getCount() { //
TODO Auto-generated method stub return
people.size(); } @Override public
Object getItem( int
position) { //
TODO Auto-generated method stub return
people.get(position); } @Override public
long
getItemId( int
position) { //
TODO Auto-generated method stub return
position; } @Override public
View getView( int
position, View convertView, ViewGroup parent) { //
TODO Auto-generated method stub People
p = people.get(position); if (convertView== null ){ convertView
= inflater.inflate(R.layout.yd_item, null ); } TextView
tv1=(TextView)convertView.findViewById(R.id.ydtext1); TextView
tv2=(TextView)convertView.findViewById(R.id.ydtext2); tv1.setText(p.getName()); tv2.setText(p.getPhome()); return
convertView; } } |
实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package
com.example.demo; public
class
People { private
String Name ; private
String Phome; public
String getName() { return
Name; } public
void
setName(String name) { Name
= name; } public
String getPhome() { return
Phome; } public
void
setPhome(String phome) { Phome
= phome; } public
People(String name,String phone){ super(); this .Name
= name; this .Phome
= phone; } } |