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>
<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>
要让数据有过滤功能,我们需要在继承的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的各个属性的前缀做比较。也可以用更加复杂的匹配,例如正则表达式。
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>
欢迎扫描二维码,关注公众号