How to add a custom adapter to an AutoCompleteTextView

前端 未结 6 1133
感情败类
感情败类 2021-02-14 08:35

Is there any simple way to set a 2 TextView dropdown to an AutoCompleteTextView.

There is android.R.layout.two_line_list_item Which I couldn\'t find any exa

6条回答
  •  悲哀的现实
    2021-02-14 09:16

    Here Code is working for me,

    Set This adapter to autocompletetextview

    AutoCompleteTextView etProductSearch = (AutoCompleteTextView)getView().findViewById(R.id.edtSearchBoxTakeOrder);
    ProductSearchAdapter adapter = new ProductSearchAdapter(getActivity(), android.R.layout.simple_dropdown_item_1line, productList);
    etProductSearch.setAdapter(adapter );
    

    ProductSearchAdapter class

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Filter;
    import android.widget.TextView;
    
    public class ProductSearchAdapter extends ArrayAdapter {
        private ArrayList items;
        private ArrayList itemsAll;
        private ArrayList suggestions;
        private int viewResourceId;
    
        @SuppressWarnings("unchecked")
        public ProductSearchAdapter(Context context, int viewResourceId,
                ArrayList items) {
            super(context, viewResourceId, items);
            this.items = items;
            this.itemsAll = (ArrayList) items.clone();
            this.suggestions = new ArrayList();
            this.viewResourceId = viewResourceId;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(viewResourceId, null);
            }
            ProductDataModel product = items.get(position);
            if (product != null) {
                  TextView productLabel = (TextView)  v.findViewById(android.R.id.text1);
                if (productLabel != null) {
                    productLabel.setText(product.getProductName());
                }
            }
            return v;
        }
    
        @Override
        public Filter getFilter() {
            return nameFilter;
        }
    
        Filter nameFilter = new Filter() {
            public String convertResultToString(Object resultValue) {
                String str = ((ProductDataModel) (resultValue)).getProductName();
                return str;
            }
    
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                if (constraint != null) {
                    suggestions.clear();
                    for (ProductDataModel product : itemsAll) {
                        if (product.getProductName().toLowerCase()
                                .startsWith(constraint.toString().toLowerCase())) {
                            suggestions.add(product);
                        }
                    }
                    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) {
                @SuppressWarnings("unchecked")
                ArrayList filteredList = (ArrayList) results.values;
                if (results != null && results.count > 0) {
                    clear();
                    for (ProductDataModel c : filteredList) {
                        add(c);
                    }
                    notifyDataSetChanged();
                }
            }
        };
    
    }
    

提交回复
热议问题