1. 程式人生 > >Android利用Filter過濾資料

Android利用Filter過濾資料

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>