Android實現ListView過濾功能,繼承於BaseAdapter,非ArrayAdapter。


 

其實實現ListView過濾功能最方便的便是使用ArrayAdapter,里面自帶的getFilter()方法能很方便的實現此功能,但是在實際的開發中,一般都是繼承於BaseAdapter。還有一種是利用控件AutoComplete,這種方式只是在輸入框的下方重新顯示一個列表,顯然,很多時候這兩種方式也滿足不了我們的要求。

在Activity中定義一個類,讓它實現TextWatcher接口,然后再onTextChanged方法中去過濾。然后常見相應的Pattern和match,來判斷傳入的參數時候符合列表中的數據,符合就加入一個新的列表中。

首先貼出實現的效果圖

 下面貼出主要實現代碼

View Code
 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

View Code
 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 }

實體類

View Code
 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 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM