Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a custom list adapter with the AutoCompleteTextView and still keeping the functionality of the auto complete working the same

So say I use the following adapter for the AutoCompleteTextView:

public class RosterAdapter extends ArrayAdapter<Player> {
...

}

That's using an object called Player, where as the default AutoCompleteTextView works with a String.

The list displays fine when I use the custom one, but the only issue I have is when I start typing something, it doesn't display the right things.

For example - if I start typing bo, I would expect people with the name Bob Henderson, Garry Bobrinski, etc..

But what comes up is the same list, which doesn't seem to matter what I type - just randomly comes up.

Can I not use a custom object for this to work? Do I have to use a String for it to match the entries properly? Or is there someway I can tell it to look at a specific string for each of the entries?

* Update *

Here's some more code - this is how I set the custom adapter RosterAdapter. This works, but the autocomplete aspect of it doesn't function properly. It's almost like it gets confused and doesn't know what to look for in the object, to match the typed string.

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_players);
            RosterAdapter adapter = new RosterAdapter(RosterActivity.this, R.layout.roster_row, players);
            textView.setAdapter(adapter);

This is using a generic ArrayAdapter, and this works fine for matching the entries:

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_players);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(RosterActivity.this, R.layout.players_autocomplete, players);
            textView.setAdapter(adapter);
like image 470
xil3 Avatar asked May 24 '11 21:05

xil3


People also ask

Which method is used for filling the data to AutoCompleteTextView?

Android AutoCompleteTextView OverviewgetAdapter() : This method returns a filterable list adapter used for auto completion.

How do I create an auto complete text view in XML?

In android, we can create an AutoCompleteTextView control in two ways either manually in an XML file or create it in the Activity file programmatically. First we create a new project by following the below steps: Click on File, then New => New Project. After that include the Kotlin support and click on next.

What does Android completion in attribute in AutoCompleteTextView does?

An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

Which properties can control type of character in AutoCompleteTextView control?

The Threshold property of AutoCompleteTextView is used to define the minimum number of characters the user must type to see the list of suggestions. The dropdown list of suggestions can be closed at any time in case if no item is selected from the list or by pressing the back or enter key.


1 Answers

It's hard to say for sure without any code, but I believe you might not be implementing getFilter() for your to let the adapter use the Player objects as strings.

For an example (unrelated requirement, but same filter needed), see: How do I Use AutoCompleteTextView and populate it with data from a web API?

There's another example here: http://www.sacoskun.com/2008/08/autocompletetextview-with-simpleadapter.html

like image 190
Aleadam Avatar answered Oct 05 '22 06:10

Aleadam