主要原理:是在主界面有兩個空間,一個是EditText,一個是ListView,ListView是放在EditText下麵的,然後自定義建立一個adapter適配器, 這個適配器要繼承Filterable這個介面,並實現這個介面的兩個方法,一個是過濾方法,一個是過濾後的方法,一般我們是在過濾方法裡面 ...
主要原理:是在主界面有兩個空間,一個是EditText,一個是ListView,ListView是放在EditText下麵的,然後自定義建立一個adapter適配器,
這個適配器要繼承Filterable這個介面,並實現這個介面的兩個方法,一個是過濾方法,一個是過濾後的方法,一般我們是在過濾方法裡面實現過濾,
在過濾後的方法裡面調用notifyDataSetChanged();也就是使適配器更新數據,這裡還有用到一個pinyin4j-2.5.0.jar的架包,這個主要是漢字轉拼
音的功能
MainActivity.Java
public class MainActivity extends Activity { private EditText et_filter; private UserAdapter adapter; private ListView lv_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<UserInfo> userInfos = new ArrayList<UserInfo>(); for (int i = 0; i < 2; i++) { UserInfo user = new UserInfo(); user.setUsername("張三" + i); user.setPhonenum("12345" + i); userInfos.add(user); } for (int i = 0; i < 2; i++) { UserInfo user = new UserInfo(); user.setUsername("李四" + i); user.setPhonenum("12345" + i); userInfos.add(user); } lv_list = (ListView) findViewById(R.id.lv_list); adapter = new UserAdapter(MainActivity.this, userInfos); lv_list.setAdapter(adapter); et_filter = (EditText) findViewById(R.id.et_filter); et_filter.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { adapter.getFilter().filter(et_filter.getText().toString()); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } }
UserAdapter.java
package com.example.edittextfilterdemo; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Filter; import android.widget.Filterable; import android.widget.TextView; /** * 自定義具有過濾功能的adapter類 * */ public class UserAdapter extends BaseAdapter implements Filterable { private MyFilter myFilter; private List<UserInfo> userInfos; private Context context; private ArrayList<UserInfo> mOriginalValues; private final Object mLock = new Object(); public UserAdapter(Context context, List<UserInfo> userInfos) { this.context = context; this.userInfos = userInfos; } @Override public int getCount() { // TODO Auto-generated method stub return userInfos.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return userInfos.get(arg0); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; ViewHolder holder; if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.list_item, null); holder = new ViewHolder(); holder.tv_nick = (TextView) view.findViewById(R.id.tv_nick); holder.tv_mobile = (TextView) view.findViewById(R.id.tv_mobile); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.tv_nick.setText(userInfos.get(position).getUsername()); holder.tv_mobile.setText(userInfos.get(position).getPhonenum()); return view; } static class ViewHolder { TextView tv_nick; TextView tv_mobile; } @Override public Filter getFilter() { if (myFilter == null) { myFilter = new MyFilter(); } return myFilter; } class MyFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence prefix) { // 持有過濾操作完成之後的數據。該數據包括過濾操作之後的數據的值以及數量。 count:數量 values包含過濾操作之後的數據的值 FilterResults results = new FilterResults(); if (mOriginalValues == null) { synchronized (mLock) { // 將list的用戶 集合轉換給這個原始數據的ArrayList mOriginalValues = new ArrayList<UserInfo>(userInfos); } } if (prefix == null || prefix.length() == 0) { synchronized (mLock) { ArrayList<UserInfo> list = new ArrayList<UserInfo>( mOriginalValues); results.values = list; results.count = list.size(); } } else { // 做正式的篩選 String prefixString = prefix.toString().toLowerCase(); // 聲明一個臨時的集合對象 將原始數據賦給這個臨時變數 final ArrayList<UserInfo> values = mOriginalValues; final int count = values.size(); // 新的集合對象 final ArrayList<UserInfo> newValues = new ArrayList<UserInfo>( count); for (int i = 0; i < count; i++) { // 如果姓名的首碼相符或者電話相符就添加到新的集合 final UserInfo value = (UserInfo) values.get(i); Log.i("coder", "PinyinUtils.getAlpha(value.getUsername())" + PinyinUtils.getFirstSpell(value.getUsername())); if (PinyinUtils.getFirstSpell(value.getUsername()).startsWith( prefixString) || value.getPhonenum().startsWith(prefixString)||value.getUsername().startsWith(prefixString)) { newValues.add(value); } } // 然後將這個新的集合數據賦給FilterResults對象 results.values = newValues; results.count = newValues.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { // 重新將與適配器相關聯的List重賦值一下 userInfos = (List<UserInfo>) results.values; if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } }
activity_main.xml:
<LinearLayout 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:orientation="vertical" tools:context="com.example.edittextfilterdemo.MainActivity" > <EditText android:id="@+id/et_filter" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/lv_list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>