Java 如何在Android中加速AutocompleteTextView?

Java 如何在Android中加速AutocompleteTextView?,java,android,autocompletetextview,trie,Java,Android,Autocompletetextview,Trie,大家好,我有一个适配器,它扩展了ArrayAdapter类并重写了一些可过滤的方法。当用户在AutocompleteTextView中键入内容时,我使用此适配器执行一些过滤。但我看到,如果你打字快一点,过滤项目的升级就会变得非常慢。这是适配器类: public class MunicipalitySearchAdapter extends ArrayAdapter<Municipality> { private ArrayList<Municipality> munic

大家好,我有一个适配器,它扩展了ArrayAdapter类并重写了一些可过滤的方法。当用户在AutocompleteTextView中键入内容时,我使用此适配器执行一些过滤。但我看到,如果你打字快一点,过滤项目的升级就会变得非常慢。这是适配器类:

public class MunicipalitySearchAdapter extends ArrayAdapter<Municipality> {

private ArrayList<Municipality> municipalities;
private ArrayList<Municipality> allMunicipalities;
private ArrayList<Municipality> suggestedMunicipalities;
private int viewResourceId;

@SuppressWarnings("unchecked")
public MunicipalitySearchAdapter(Context context, int viewResourceId, ArrayList<Municipality> municipalities) {
    super(context, viewResourceId, municipalities);
    this.municipalities = municipalities;
    this.allMunicipalities = (ArrayList<Municipality>) this.municipalities.clone();
    this.suggestedMunicipalities = new ArrayList<Municipality>();
    this.viewResourceId = viewResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(this.viewResourceId, null);
    }
    Municipality municipality = municipalities.get(position);
    if (municipality != null) {
        TextView munNameTxtView = (TextView) v.findViewById(R.id.name);
        TextView proSignTxtView = (TextView) v.findViewById(R.id.sign);
        if (munNameTxtView != null) {
            munNameTxtView.setText(municipality.getName());
        }
        if (proSignTxtView != null) {
            proSignTxtView.setText(municipality.getProvinceSign());
        }
 }
    return v;
}


@Override 
public Filter getFilter() {
    return municipalityFilter;
}

Filter municipalityFilter = new Filter() {

    @Override
    public String convertResultToString(Object resultValue) {
        String str = ((Municipality) (resultValue)).getName();
        return str;
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {
            suggestedMunicipalities.clear();
            for (Municipality municipality : allMunicipalities) {
                if (municipality.getName().toLowerCase(Locale.getDefault()).startsWith(constraint.toString().toLowerCase(Locale.getDefault()))) {
                    suggestedMunicipalities.add(municipality);
                }
            }
            FilterResults filterRes = new FilterResults();
            filterRes.values = suggestedMunicipalities;
            filterRes.count = suggestedMunicipalities.size();
            return filterRes;
        }
        else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        if (results != null && results.count > 0) {
            @SuppressWarnings("unchecked")
            ArrayList<Municipality> filteredMunicipalities = (ArrayList<Municipality>) results.values;
            ArrayList<Municipality> supportMunicipalitiesList = new ArrayList<Municipality>();

            clear();
            for (Municipality mun : filteredMunicipalities) {
                supportMunicipalitiesList.add(mun);
            }
            Iterator<Municipality> municipalityIterator = supportMunicipalitiesList.iterator();
            while (municipalityIterator.hasNext()) {
                Municipality municipality = municipalityIterator.next();
                add(municipality);
            }
            notifyDataSetChanged();
        }           
    }
};
 }
公共类市政当局ArrayAdapter扩展了ArrayAdapter{
私营ArrayList市镇;
所有市政当局的私营ArrayList;
私人ArrayList建议的市政设施;
私有int-viewResourceId;
@抑制警告(“未选中”)
公共市政当局AsharAdapter(上下文、int-viewResourceId、ArrayList市政当局){
超级(上下文、viewResourceId、城市);
这个城市=城市;
this.alluscities=(ArrayList)this.uscities.clone();
this.suggestedMunicipalites=newArrayList();
this.viewResourceId=viewResourceId;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater充气器=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u充气器\u SERVICE);
v=充气器.inflate(this.viewResourceId,null);
}
市政当局=市政当局.get(位置);
如果(市政当局!=null){
TextView munname txtView=(TextView)v.findViewById(R.id.name);
TextView proSignTxtView=(TextView)v.findViewById(R.id.sign);
if(munNameTxtView!=null){
munNameTxtView.setText(市政当局.getName());
}
if(proSignTxtView!=null){
proSignTxtView.setText(市政当局.getProvinceSign());
}
}
返回v;
}
@凌驾
公共过滤器getFilter(){
返回市政过滤器;
}
过滤器市政过滤器=新过滤器(){
@凌驾
公共字符串ConvertResultString(对象结果值){
字符串str=((市政)(结果值)).getName();
返回str;
}
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
if(约束!=null){
suggestedMunicipalities.clear();
适用于(直辖市:所有直辖市){
if(university.getName().toLowerCase(Locale.getDefault()).startsWith(constraint.toString().toLowerCase(Locale.getDefault())){
建议城市。添加(城市);
}
}
FilterResults filterRes=新的FilterResults();
filterRes.values=建议的城市权限;
filterRes.count=suggestedmuniciprities.size();
返回过滤器;
}
否则{
返回新的FilterResults();
}
}
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
if(results!=null&&results.count>0){
@抑制警告(“未选中”)
ArrayList FilteredMunicipalites=(ArrayList)results.values;
ArrayList Support市政列表=新建ArrayList();
清除();
适用于(市政:过滤市政){
支持市政列表。添加(mun);
}
迭代器市政迭代器=支持市政迭代器列表。迭代器();
while(市政当局iterator.hasNext()){
市政当局=市政当局iterator.next();
增补(直辖市);
}
notifyDataSetChanged();
}           
}
};
}
我想问一下,是否有人知道如何提高这种AutocompleteTextView的性能,并使更新更快。我该怎么办?谢谢

编辑:我创建了以下类: 顶点: 公共类顶点{

private HashMap<Character, Vertex> vertexSons;
private List<Integer> wordsIndexList;
private List<Integer> prefixesIndexList;
private int wordsNumber;
private int prefixesNumber;

public Vertex() {
    vertexSons = new HashMap<Character, Vertex>();
    wordsIndexList = new ArrayList<Integer>();
    prefixesIndexList = new ArrayList<Integer>();
    wordsNumber = 0;
    prefixesNumber = 0;
}

public boolean hasWords() {
    if (wordsNumber > 0) {
        return true;
    }
    return false;
}

public boolean hasPrefixes() {
    if (prefixesNumber > 0) {
        return true;
    }
    return false;
}

public void addVertexSon(Character character) {
    vertexSons.put(character, new Vertex());
}

public void addIndexToWordsIndexList(int index) {
    wordsIndexList.add(index);
}

public void addIndexToPrefixesIndexList(int index) {
    prefixesIndexList.add(index);
}

public HashMap<Character, Vertex> getVertexSons() {
    return vertexSons;
}

public List<Integer> getWordsIndexList() {
    return wordsIndexList;
}

public List<Integer> getPrefixesIndexList() {
    return prefixesIndexList;
}

public int getWordsNumber() {
    return wordsNumber;
}

public int getPrefixesNumber() {
    return prefixesNumber;
}

public void increaseWordsNumber() {
    wordsNumber++;
}

public void increasePrefixesNumber() {
    prefixesNumber++;
}
}
私有HashMap vertexson;
私有列表词索引列表;
私有列表前缀索引列表;
私有int字数;
私有int前缀数目;
公共顶点(){
vertexSons=新HashMap();
wordsIndexList=newarraylist();
prefixesIndexList=新的ArrayList();
字数=0;
prefixesNumber=0;
}
公共布尔hasWords(){
如果(字数>0){
返回true;
}
返回false;
}
公共布尔前缀(){
如果(前缀数目>0){
返回true;
}
返回false;
}
公共void addVertexSon(字符){
put(字符,新顶点());
}
公共无效addIndexToWordsIndexList(整数索引){
添加(索引);
}
public void addIndexToPrefixesIndexList(int索引){
前缀index list.add(索引);
}
公共HashMap getVertexSons(){
返回顶点;
}
公共列表getWordsIndexList(){
返回单词索引列表;
}
公共列表getPrefixesIndexList(){
返回前缀索引列表;
}
public int getWordsNumber(){
返回字数;
}
public int getPrefixesNumber(){
返回前缀数目;
}
public void increaseWordsNumber(){
wordsNumber++;
}
public void increasePrefixesNumber(){
前缀数++;
}
}
和Trie:

public class Trie {

private Vertex rootVertex;

public Trie(List<Trieable> objectList, Locale locale) {
    rootVertex = new Vertex();

    for (int i = 0; i<objectList.size(); i++) {
        String word = objectList.get(i).toString().toLowerCase(locale);
        addWord(rootVertex, word, i);
    }
}

public Vertex getRootVertex() {
    return rootVertex;
}

public void addWord(Vertex vertex, String word, int index) {
    if (word.isEmpty()) { 
        vertex.addIndexToWordsIndexList(index);
        vertex.increaseWordsNumber();
    }
    else {
        vertex.addIndexToPrefixesIndexList(index);
        vertex.increasePrefixesNumber();
        Character fChar = word.charAt(0);
        HashMap<Character, Vertex> vertexSons = vertex.getVertexSons();

        if (!vertexSons.containsKey(fChar)) {
            vertex.addVertexSon(fChar);
        }

        word = (word.length() == 1) ? "" : word.substring(1);
        addWord(vertexSons.get(fChar), word, index);
    }
}

public List<Integer> getWordsIndexes(Vertex vertex, String word) {
    if (word.isEmpty()) {
        return vertex.getWordsIndexList();
    }
    else {
        Character fChar = word.charAt(0);
        if (!(vertex.getVertexSons().containsKey(fChar))) {
            return null;
        }
        else {
            word = (word.length() == 1) ? "" : word.substring(1);
            return getWordsIndexes(vertex.getVertexSons().get(fChar), word);
        }
    }
}

public List<Integer> getPrefixesIndexes(Vertex vertex, String prefix) {
    if (prefix.isEmpty()) {
        return vertex.getWordsIndexList();
    }
    else {
        Character fChar = prefix.charAt(0);
        if (!(vertex.getVertexSons().containsKey(fChar))) {
            return null;
        }
        else {
            prefix = (prefix.length() == 1) ? "" : prefix.substring(1);
            return getWordsIndexes(vertex.getVertexSons().get(fChar), prefix);
        }
    }
}

}
公共类Trie{
私有顶点根顶点;
公共Trie(列表对象列表、区域设置){
rootVertex=新顶点();
对于(int i=0;i 0){
@抑制警告(“未选中”)
ArrayList FilteredMunicipalites=(ArrayList)results.values;
ArrayList Support市政列表=新建ArrayList();
清除();
适用于(市政:过滤市政){
支持市政列表。添加(mun);
}
迭代器市政迭代器=支持市政迭代器列表。迭代器();
while(市政当局iterator.hasNext()){
市政当局=市政当局iterator.next();
增补(直辖市);
}
notifyDataSetChanged();
}           
}
};
现在我得到一个空指针警告
Filter municipalityFilter = new Filter() {



    @Override
    public String convertResultToString(Object resultValue) {
        String str = ((Municipality) (resultValue)).getName();
        return str;
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        if (constraint != null) {
            String constraintString = constraint.toString().trim();
            suggestedMunicipalities.clear();

            List<Integer> wordsIndexesList = municipalityTrie.getWordsIndexes(municipalityTrie.getRootVertex(), constraintString);
            for (int index : wordsIndexesList) {
                suggestedMunicipalities.add(allMunicipalities.get(index));
            }

            List<Integer> prefixesIndexesList = municipalityTrie.getPrefixesIndexes(municipalityTrie.getRootVertex(), constraintString);
            for (int index : prefixesIndexesList) {
                suggestedMunicipalities.add(allMunicipalities.get(index));
            }

            FilterResults filterRes = new FilterResults();
            filterRes.values = suggestedMunicipalities;
            filterRes.count = suggestedMunicipalities.size();
            return filterRes;
        }
        else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        if (results != null && results.count > 0) {
            @SuppressWarnings("unchecked")
            ArrayList<Municipality> filteredMunicipalities = (ArrayList<Municipality>) results.values;
            ArrayList<Municipality> supportMunicipalitiesList = new ArrayList<Municipality>();

            clear();
            for (Municipality mun : filteredMunicipalities) {
                supportMunicipalitiesList.add(mun);
            }
            Iterator<Municipality> municipalityIterator = supportMunicipalitiesList.iterator();
            while (municipalityIterator.hasNext()) {
                Municipality municipality = municipalityIterator.next();
                add(municipality);
            }
            notifyDataSetChanged();
        }           
    }
};
List<Integer> wordsIndexesList = municipalityTrie.getWordsIndexes(municipalityTrie.getRootVertex(), constraintString);
            for (int index : wordsIndexesList) {
                suggestedMunicipalities.add(allMunicipalities.get(index));
            }