In fact, the most convenient way to achieve ListView filtering is to use Arrayadapter, which comes with the GetFilter () method can be very convenient to achieve this function, but in the actual development, is generally inherited from the baseadapter. There is also the use of Control AutoComplete, which is simply to display a list at the bottom of the input box, obviously, these two ways are not enough to meet our requirements.
Define a class in the activity, let it implement the Textwatcher interface, and then filter through the OnTextChanged method. The corresponding pattern and match are then commonly used to determine if the incoming parameters match the data in the list and join a new list.
First put out the implementation of the effect diagram
The main implementation code is posted below
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 ("John", "1374456"));
People.add (New people ("John Boy", "12444455"));
People.add (New people ("Li Yi Chan", "1345555"));
People.add (New people ("Wang Yi", "1355555"));
People.add (New People ("King two", "1365555"));
People.add (New people ("Lie triple Systems", "13565555"));
People.add (New people ("Li Yi Chan", "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 Me Thod stub} @Override public void ontextchanged (charsequence s, int star
T, 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 men U
This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true; }
}