MainActivity如下:
package cc.testfilterable; import java.util.ArrayList; import java.util.HashMap; import android.os.Bundle; import android.widget.ListView; import android.app.Activity; import android.content.Context; /** * Demo描述: * 利用Filter过滤数据 * 可用于AutoCompleteTextView筛选数据等功能 * * 备注说明: * 该Demo只表述原理,代码逻辑较简单,有一些小的bug. * 完整的应用请参见以下资料. * * 参考资料: * 1 http://gundumw100.iteye.com/blog/1446507 * 2 http://blog.csdn.net/ssstudio/article/details/7579089 * 3 http://blog.csdn.net/jiahui524/article/details/7802033 * */ public class MainActivity extends Activity { private Context mContext; private ListView mListView; private ListViewAdapter mListViewAdapter; private ArrayList<String> mArrayList; private HashMap<String, String> mHashMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mContext=this; mListView=(ListView) findViewById(R.id.listView); mArrayList=new ArrayList<String>(); mArrayList.add("张三1"); mArrayList.add("李四1"); mArrayList.add("王五1"); mArrayList.add("张三2"); mArrayList.add("李四2"); mArrayList.add("王五2"); mArrayList.add("张三3"); mArrayList.add("李四3"); mArrayList.add("王五3"); mArrayList.add("张三4"); mArrayList.add("李四4"); mArrayList.add("王五4"); mListViewAdapter=new ListViewAdapter(mContext, mArrayList); //过滤数据 //过滤出姓名里面包含"张"的数据 mListViewAdapter.getFilter().filter("张"); mListView.setAdapter(mListViewAdapter); } }
ListViewAdapter如下:
package cc.testfilterable; import java.util.ArrayList; import java.util.Iterator; 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.Filter; import android.widget.TextView; public class ListViewAdapter extends BaseAdapter { private NameFilter mNameFilter; private List<String> mArrayList; private List<String> mFilteredArrayList; private LayoutInflater mLayoutInflater; public ListViewAdapter(Context context,List<String> arrayList) { mArrayList = arrayList; mLayoutInflater=LayoutInflater.from(context); mFilteredArrayList=new ArrayList<String>(); } @Override public int getCount() { if (mArrayList == null) { return 0; } else { return (mArrayList.size()); } } @Override public Object getItem(int position) { if (mArrayList == null) { return null; } else { return mArrayList.get(position); } } @Override public long getItemId(int position) { return position; } public Filter getFilter() { if (mNameFilter == null) { mNameFilter = new NameFilter(); } return mNameFilter; } @Override public View getView(int position, View convertView, ViewGroup parent) { View itemView = null; itemView = convertView; ViewHolder viewHolder = null; if (itemView == null) { itemView = mLayoutInflater.inflate(R.layout.listviewitem, null); viewHolder = new ViewHolder(); viewHolder.textView = (TextView) itemView.findViewById(R.id.textView); itemView.setTag(viewHolder); } else { viewHolder = (ViewHolder) itemView.getTag(); } if (mArrayList != null) { if (viewHolder.textView != null) { viewHolder.textView.setText((mArrayList.get(position))); } } convertView = itemView; return convertView; } private class ViewHolder { TextView textView; } //过滤数据 class NameFilter extends Filter { //执行筛选 @Override protected FilterResults performFiltering(CharSequence charSequence) { FilterResults filterResults = new FilterResults(); for (Iterator<String> iterator = mArrayList.iterator(); iterator.hasNext();) { String name = iterator.next(); System.out.println("---> name=" + name); if (name.contains(charSequence)) { mFilteredArrayList.add(name); } } filterResults.values = mFilteredArrayList; return filterResults; } //筛选结果 @Override protected void publishResults(CharSequence arg0, FilterResults results) { mArrayList = (List<String>) results.values; if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } }} }
<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" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="50dip" android:gravity="center" /> </LinearLayout>
时间: 2024-10-12 13:04:03