Filtering AutoCompleteTextView to show partial match

给你一囗甜甜゛ 提交于 2020-01-17 03:23:09

问题


Right now I have an AutoCompleteTextView and I want it to work just like the like "%xxx%" does in SQL. I attempted to do it using Filterable I have it and the code runs but it just displays everything now even if there is no partial match. Any help would be appreciated.

public class CodesArrayAdapter extends ArrayAdapter implements Filterable{

    List<String> allCodes;
    List<String> originalCodes;

    StringFilter filter;


    public CodesArrayAdapter(Context context, int resource, List<String> keys) {
        super(context, resource, keys);

        allCodes=keys;
        originalCodes=keys;


    }

    public int getCount() {
        return allCodes.size();
    }

    public Object getItem(int position) {
        return allCodes.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    private class StringFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<String> list = originalCodes;

            int count = list.size();
            final ArrayList<String> nlist = new ArrayList<String>(count);

            String filterableString ;

            for (int i = 0; i < count; i++) {
                filterableString = list.get(i);
                if (filterableString.toLowerCase().contains(filterString)) {
                    nlist.add(filterableString);
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            allCodes = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

    }
}

回答1:


If this is the complete code of your adapter, you just missed implementing the getFilter() method, i.e.

@Override
public Filter getFilter()
{
    return new StringFilter();
}


来源:https://stackoverflow.com/questions/24313364/filtering-autocompletetextview-to-show-partial-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!