【问题标题】:Filter special characters for AutoCompleteTextView Android过滤 AutoCompleteTextView Android 的特殊字符
【发布时间】:2016-03-30 03:00:02
【问题描述】:

我有一个字符串数组,例如:-

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

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

 ArrayAdapter adapter = new ArrayAdapter<System.String> (this, Resource.Layout.rout_row, vehicleArray);

    autoCompleteBusStop.Adapter = adapter;

我面临的问题是,如果我输入 India,项目会正确显示,但如果我输入“Malaysia”或“Singapore”,项目不会显示,可能是因为“-Malaysia”被视为一个词。如果我输入“-”,项目就会显示。即使我输入了马来西亚,我如何搜索?

【问题讨论】:

    标签: android special-characters autocompletetextview


    【解决方案1】:

    您可以为此使用自定义适配器

    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();
        }
    
    }
    
    }

    【讨论】:

    • 它给了我一个错误results.values = nlist;Cannot implicitly convert type 'System.Collections.Generic.List&lt;string&gt;' to 'Java.Lang.Object'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2016-06-24
    • 2019-05-25
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多