代码之家  ›  专栏  ›  技术社区  ›  sunil kushwah

如何显示自动完成文本视图的建议列表,该文本视图仅显示以输入字符开头的单词

  •  0
  • sunil kushwah  · 技术社区  · 8 年前

    我想显示建议列表,其中只包含以输入字符开头的单词,而不是包含输入字符的单词。

    例如,假设我键入 L 然后它会给我一个列表,比如

    ["Launga Beach","Launga Hills","Launga Woods"] 
    

    不是这样的

    ["Echo Lake,"Fields Landing","Green Vally Lake"]
    

    这是我的密码

     ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(getActivity(),R.layout.auto_complete_select_dialog_item,mCityZipCodeMasterForCity.getCity());
            cityAutoCompleteTextView.setThreshold(1);
            cityAutoCompleteTextView.setAdapter(cityAdapter);
            cityAutoCompleteTextView.setOnFocusChangeListener(new AutoCompleteTextValidator());
    

    在android的自动完成文本视图中,是否有任何方法可以实现这一点?

    谢谢

    2 回复  |  直到 8 年前
        1
  •  1
  •   AndiGeeky    8 年前

    下面我添加了一个示例 Adapter 供您参考 Custom 滤器

    你只需要你的过滤逻辑。

    CustomAdapter.java

    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Filter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class CustomAdapter extends ArrayAdapter<String> {
    
        Context context;
        int resource, textViewResourceId;
        List<String> items, tempItems, suggestions;
    
        public CustomAdapter(Context context, int resource, int textViewResourceId, List<String> items) {
            super(context, resource, textViewResourceId, items);
            this.context = context;
            this.resource = resource;
            this.textViewResourceId = textViewResourceId;
            this.items = items;
            tempItems = new ArrayList<String>(items); // this makes the difference.
            suggestions = new ArrayList<String>();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            // Handle your layout here
            return view;
        }
    
        @Override
        public Filter getFilter() {
            return nameFilter;
        }
    
        /**
         * Custom Filter implementation for custom suggestions we provide.
         */
        Filter nameFilter = new Filter() {
            @Override
            public CharSequence convertResultToString(Object resultValue) {
                return resultValue.toString();
            }
    
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                if (constraint != null) {
                    suggestions.clear();
    
                    // Here you can add your pattern like L* for starting string with 'L'
                    for (String item : tempItems) {
                        if (item.startsWith("your pattern here")) {
                            suggestions.add(item);
                        }
                    }
                    FilterResults filterResults = new FilterResults();
                    filterResults.values = suggestions;
                    filterResults.count = suggestions.size();
                    return filterResults;
                } else {
                    return new FilterResults();
                }
            }
    
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                List<String> filterList = (ArrayList<String>) results.values;
                if (results != null && results.count > 0) {
                    clear();
                    for (String item : filterList) {
                        add(item);
                        notifyDataSetChanged();
                    }
                }
            }
        };
    }
    

    谢谢

        2
  •  0
  •   Rahul    8 年前

    有字符串的方法

    public boolean startsWith(String prefix)
    

    实例

    String text = "searching my text";
    boolean value = text.startsWith("searching"); // return true
    boolean value1 = text.startsWith("my"); // return false
    

    因为忽略了这个案子

    boolean value = text.toLowerCase().startsWith("searching".toLowerCase());