过滤AutoCompleteTextView Android的特殊字符

时间:2015-06-02 06:14:25

标签: android special-characters autocompletetextview

我有一系列字符串,如: -

<%= options_from_collection_for_select(Product.all, :id, :get_category_name) %>

我将此数组传递给AutocompleteTextView,如下所示: -

public static string[] vehicleArray = string[]{"01:00 India-Malaysia","02:00 India-Singapore","03:00 India-Malaysia"};

我面临的问题是,如果我输入印度项目显示正确,但如果我输入“马来西亚”或“新加坡”,则项​​目可能不会显示,因为“-Malaysia”被视为一个单词。如果我输入“ - ”项目显示。即使我输入马来西亚,我该如何搜索?

1 个答案:

答案 0 :(得分:0)

您可以使用自定义适配器,如

public class CustomAdapter extends BaseAdapter implements Filterable {


private Context context;


private List<String> originalData;
private List<String> filteredData;

private ItemFilter mFilter = new ItemFilter();

public CustomAdapter(List<String> itemList, Context ctx) {

    originalData = new ArrayList<String>();
    filteredData = new ArrayList<String>();

    filteredData = itemList;
    originalData = itemList;

    context = ctx;
}

@Override
public int getCount() {
    return filteredData.size();
}

@Override
public Object getItem(int position) {
    return filteredData.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(android.R.layout.simple_dropdown_item_1line, null);
    }

    final TextView text = (TextView) convertView.findViewById(android.R.id.textView);

    text.setText(filteredData.get(position));

    return convertView;
}

public Filter getFilter() {
    return mFilter;
}

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

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

        Filter.FilterResults results = new FilterResults();

        final List<String> list = originalData;

        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) {
        filteredData = (ArrayList<String>) results.values;
        notifyDataSetChanged();
    }

}

}