自定义AutoCompleteTextView Android

时间:2014-09-19 22:17:04

标签: android android-edittext autocompletetextview custom-adapter

我想在Android中自定义AutoCompleteTextView。通常,只有当我们开始在EditText中键入文本时,才会弹出下拉列表。但是,当我单击AutoCompleteTextView时,我想显示所有元素,然后在我开始键入文本时显示已过滤的元素。我应该采用什么方法来实现这一目标。

3 个答案:

答案 0 :(得分:1)

此处代码正在为我工​​作,

将此适配器设置为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类

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<ProductDataModel> {
    private ArrayList<ProductDataModel> items;
    private ArrayList<ProductDataModel> itemsAll;
    private ArrayList<ProductDataModel> suggestions;
    private int viewResourceId;

    @SuppressWarnings("unchecked")
    public ProductSearchAdapter(Context context, int viewResourceId,
            ArrayList<ProductDataModel> items) {
        super(context, viewResourceId, items);
        this.items = items;
        this.itemsAll = (ArrayList<ProductDataModel>) items.clone();
        this.suggestions = new ArrayList<ProductDataModel>();
        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<ProductDataModel> filteredList = (ArrayList<ProductDataModel>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (ProductDataModel c : filteredList) {
                    add(c);
                }
                notifyDataSetChanged();
            }
        }
    };

}

答案 1 :(得分:0)

创建AutoComplteText类的子类并覆盖enoughToFilter方法,如下所示:

@Override
public boolean enoughToFilter() {
    return true;
}

答案 2 :(得分:0)

我确实这样:

mAutocompleteTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            ((AutoCompleteTextView) v).showDropDown();
        }
    });