【问题标题】:AutoCompleteTextView does not work with custom ArrayAdapterAutoCompleteTextView 不适用于自定义 ArrayAdapter
【发布时间】:2015-10-26 21:48:34
【问题描述】:

我正在尝试实现AutoCompleteTextView 来显示自定义对象。因此,我实现了自己的 ArrayAdapter,但它不起作用,当我在文本字段中输入内容时没有显示任何建议。有人可以帮我吗?

public class AutoCompleteArrayAdapter extends ArrayAdapter<Object>{

    List<Object> mObjectList;
    Context mContext;
    LayoutInflater mInflater;
    int mResourceId;

    public AutoCompleteArrayAdapter(Context context, int resource, List<Object> objectList) {
        super(context, resource, objectList);
        mResourceId = resource;
        mObjectList = objectList;
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        if(convertView == null) {
            convertView = mInflater.inflate(mResourceId, parent, false);
        }

        Object object = mObjectList.get(position);

        TextView textViewItem = (TextView) convertView.findViewById(R.id.textView_dropDown);
        textViewItem.setText(object.getString());

        return convertView;
    }

    @Override
    public int getCount() {
        return mObjectList.size();
    }

    @Override
    public WordInfo getItem(int position) {
        return mObjectList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

这里我在主activity中设置了适配器:

    AutoCompleteArrayAdapter adapter = new AutoCompleteArrayAdapter(this, R.layout.simple_textview, mAllWords);
    mAutoEditTextSwedish.setAdapter(adapter);

【问题讨论】:

    标签: android android-arrayadapter android-adapter autocompletetextview


    【解决方案1】:

    在您的自定义ArrayAdapter 类中,您需要覆盖public Filter getFilter()

    我在以下问题上有一个工作示例代码,请看一下:

    How to create custom BaseAdapter for AutoCompleteTextView

    希望这会有所帮助!

    【讨论】:

    • 谢谢,这正是我的错误
    猜你喜欢
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 2014-04-21
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多