0
這是我的自定義ArrayAdapter定製ArrayAdapter爲自動完成,顯示所有結果
public class CustomAdapater extends ArrayAdapter implements Filterable {
private ArrayList<String> mData;
private ArrayList<String> orig;
private ArrayList<String> suggestions;
LayoutInflater inflater;
@SuppressWarnings("unchecked")
public CustomAdapater(Context context, int textViewResourceId,ArrayList<String> al) {
super(context,textViewResourceId,al);
inflater = LayoutInflater.from(context);
mData = al;
orig = (ArrayList<String>)mData.clone();
this.suggestions = new ArrayList<String>();
}
@Override
public int getCount() {
return suggestions.size();
}
@Override
public Object getItem(int position) {
return suggestions.get(position);
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint != null) {
suggestions.clear();
for(String s:orig){
if(s!=null && s.contains(constraint))
suggestions.add(s);
}
filterResults.values = suggestions;
filterResults.count = suggestions.size();
}
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
mData = (ArrayList<String>)results.values;
notifyDataSetChanged();
}
};
return myFilter;
}
}
我申請這自動完成場。默認情況下以搜索字符串開頭的自動完成搜索列表。我想要包含搜索字符串的列表。
如果這解決了您的問題或幫助..接受答案... =] – 5hssba 2012-03-14 09:37:04