I’m having issues with an Android AutoComplete dropdown menu. I am trying to pass a custom object into a new ArrayAdapter, which is then used for the data in the dropdown menu. However, I’m getting the following error for this line of code:
adapter = new ArrayAdapter<String>(_context, R.layout.list_item, list_items);
The constructor ArrayAdapter<String>(Context, int, DropdownItemData[]) is undefined
I’ve tried running this as well, which outputs a type mismatch error:
adapter = new ArrayAdapter<DropdownItemData>(_context, R.layout.list_item, list_items);
Type mismatch: cannot convert from ArrayAdapter<DropdownItemData> to ArrayAdapter<String>
However, the following does work just fine:
String[] test_list = new String[] {"some", "test", "data"};
adapter = new ArrayAdapter<String>(_context, R.layout.list_item, test_list);
My DropdownItemData class overrides the Object toString() method, as is recommended in the ArrayList documentation here:
http://developer.android.com/reference/android/widget/ArrayAdapter.html
Does anyone know what the issue is/what I’m missing? According to the docs, the constructor I’m trying to use for ArrayList should just be taking a generic list–so I don’t understand what the error is complaining about.
The class name is
ArrayAdapter<T>. And the constructor signature isArrayAdapter(Context context, int textViewResourceId, T[] objects).So if you declare your variable as an
ArrayAdapter<String>, you must pass an array ofStringas last argument of the constructor.To be able to pass it an array of
DropdownItemData, your adapter must be of typeArrayAdapter<DropdownItemData>.You can put a lion in a
Cage<Lion>, and a bird in aCage<Bird>. But you may not use aCage<Lion>with birds, nor aCage<Bird>with lions.You should read the generics tutorial.