AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字)
AutoCompleteTextView具有输入提示的功能,但是它的这种提示不适合对股票列表的过滤,如果你玩过股票软件,就会知道只要输入股票名称的首字母或股票代码就会出现符合匹配的股票,这种过滤怎么实现呢?
还有个问题,汉字具有多音字,如何实现多音字的匹配,比如“长江证券”,无论你输入“cjzq”或者“zjzq”都会匹配到它,这都是需要解决的问题!
匹配的关键在于重写BaseAdapter,让它实现Filterable接口,重写其中的getFilter(),如果你参照ArrayAdaper源码的话,写起来就会容易很多,事实上我就是这么做的,^o^
下面看一下源码:
Java代码 package com.ql.util;
import java.util.*;
import android.content.Context;
import android.util.Log;
import android.view.*;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
public class SearchAdapter<T> extends BaseAdapter implements Filterable {
private List<T> mObjects;
private List<Set<String>> pinyinList;//支持多音字,类似:{{z,c},{j},{z},{q,x}}的集合
private final Object mLock = new Object();
private int mResource;
private int mFieldId = 0;
private Context mContext;
private ArrayList<T> mOriginalValues;
private ArrayFilter mFilter;
private LayoutInflater mInflater;
public static final int ALL=-1;//全部
private int maxMatch=10;//最多显示多少个可能选项
//支持多音字
public SearchAdapter(Context context,int textViewResourceId, T[] objects,int maxMatch) {
// TODO Auto-generated constructor stub
init(context, textViewResourceId, 0, Arrays.asList(objects));
this.pinyinList = getHanziSpellList(objects);
this.maxMatch=maxMatch;
}
public SearchAdapter(Context context,int textViewResourceId, List<T> objects,int maxMatch) {
// TODO Auto-generated constructor stub
init(context, textViewResourceId, 0, objects);
this.pinyinList = getHanziSpellList(objects);
this.maxMatch=maxMatch;
}
private void init(Context context, int resource, int textViewResourceId,List<T> objects) {
mContext = context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mResource = resource;
mObjects = objects;
mFieldId = textViewResourceId;
}
/**
* 获得汉字拼音首字母列表
*/
private List<Set<String>> getHanziSpellList(T[] hanzi){
List<Set<String>> listSet=new ArrayList<Set<String>>();
PinYin4j pinyin=new PinYin4j();
for(int i=0;i<hanzi.length;i++){
listSet.add(pinyin.getPinyin(hanzi[i].toString()));
}
return listSet;
}
/**
* 获得汉字拼音首字母列表
*/
private List<Set<String>> getHanziSpellList(List<T> hanzi){
List<Set<String>> listSet=new ArrayList<Set<String>>();
PinYin4j pinyin=new PinYin4j();
for(int i=0;i<hanzi.size();i++){
listSet.add(pinyin.getPinyin(hanzi.get(i).toString()));
}
return listSet;
}
public int getCount() {
return mObjects.size();
}
public T getItem(int position) {
return mObjects.get(position);
}
public int getPosition(T item) {
return mObjects.indexOf(item);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position, convertView, parent, mResource);
}
private View createViewFromResource(int position, View convertView,
ViewGroup parent, int resource) {
View view;
TextView text;
if (convertView == null) {
view = mInflater.inflate(resource, parent, false);
} else {
view = convertView;
}
try {
if (mFieldId == 0) {
text = (TextView) view;
} else {
text = (TextView) view.findViewById(mFieldId);
}
 
补充:移动开发 , Android ,