android:自动完成结果中没有显示任何内容

时间:2014-09-22 05:10:52

标签: android autocomplete android-filterable

我已为Filterable实施了AutoCompleteTextView

public class IdiomAdapter extends BaseAdapter implements Filterable{

    Context context;
    List<Idiom> idioms;

    public IdiomAdapter(Context context, List<Idiom> idioms){
        this.context = context;
        this.idioms = idioms;
        originaList = new ArrayList<Idiom>(idioms);
        filter = new IdiomFilter();
    }

    @Override
    public int getCount() {
        if (idioms != null) {
            return idioms.size();
        }else{
            return 0;
        }
    }

    @Override
    public Object getItem(int arg0) {
        return idioms.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return idioms.get(arg0).getId();
    }



    @Override
    public Filter getFilter() {
        return filter;
    }

    private IdiomFilter filter;
    ArrayList<Idiom> originaList;

    public class IdiomFilter extends Filter {

        public IdiomFilter(){

        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults oReturn = new FilterResults();
            ArrayList<Idiom> result = new ArrayList<Idiom>();
            if (originaList == null) {
                originaList = (ArrayList<Idiom>) idioms;
            }

            if (constraint !=null) {
                if (originaList !=null && originaList.size() > 0) {
                    for (Idiom idiom : originaList) {
                        if (idiom.getVerb().contains(constraint))
                            result.add(idiom);
                    }
                }
                oReturn.values = result;
            }
            return oReturn;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            idioms = (ArrayList<Idiom>)results.values;
            notifyDataSetChanged();
        }

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View v = convertView;

        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.simple_list_item_1, null);
        }
        return v;
    }

在我的活动中使用它:

IdiomAdapter adapter = new IdiomAdapter(MainActivity.this, idioms);
AutoCompleteTextView txtSearch = (AutoCompleteTextView) findViewById(R.id.txtSearch);
txtSearch.setAdapter(adapter);

当我在AutoCompleteTextView中输入关键字时,似乎工作但结果中没有显示任何内容: enter image description here

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试setThreshold(1)的{​​{1}}。 添加以下行:

AutoCompleteTextView

使用此代码的适配器:

txtSearch.setThreshold(1);