AutoCompleteTextView 不适用于自定义 ArrayAdapter

Nat*_*tjo 3 android autocompletetextview android-arrayadapter android-adapter

我正在尝试实现一个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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我在主要活动中设置了适配器:

    AutoCompleteArrayAdapter adapter = new AutoCompleteArrayAdapter(this, R.layout.simple_textview, mAllWords);
    mAutoEditTextSwedish.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

BNK*_*BNK 7

在你的习惯 ArrayAdapter类中,您需要覆盖public Filter getFilter()

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

如何为 AutoCompleteTextView 创建自定义 BaseAdapter

希望这可以帮助!