Natjo
Natjo

Reputation: 2128

AutoCompleteTextView does not work with custom ArrayAdapter

I'm trying to implement an AutoCompleteTextView to show a custom Object. Therefore I implemented my own ArrayAdapter, but it is not working, there are no suggestions shown when I enter something in the textfield. Can someone help me?

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;
    }
}

Here I set the adapter in the main activity:

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

Upvotes: 3

Views: 9133

Answers (1)

BNK
BNK

Reputation: 24114

In your custom ArrayAdapter class, you need to override public Filter getFilter()

I have a working sample code at the following question, please take a look:

How to create custom BaseAdapter for AutoCompleteTextView

Hope this helps!

Upvotes: 7

Related Questions