Question

I am facing a weird problem with AutoCompleteTextView. My code is as follows,

    AutoCompleteTextView  searchText = //findView...
private ArrayList<String> suggestions = null;
private ArrayAdapter<String> suggestionAdapter = null;
suggestionAdapter = new ArrayAdapter<String>(this, R.layout.list, suggestions);
searchText.setAdapter(suggestionAdapter);

And down the code, I am populating the arrayList in a for loop.

    for (int i = 0; i < nl.getLength(); i++) {
        Element suggestion = (Element)nl.item(i);
        String name = suggestion.getAttribute("data");
        suggestions.add(name);
    }

This is not showing me the suggestions while I type into the text view.

However, when I add any strings to the arraylist outside the for loop (like, right after the loop), I am able to see the suggestions. Its been bugging me for the last two hours. Any suggestions would be appreciated.

And believe me I am typing one of the known text that I am populating in the for loop.

Thx! Rahul.

Was it helpful?

Solution

Do not set the adapter before the forloop. Add all the Strings to the ArrayList of Strings and thne after the for loop use this:

suggestionAdapter = new ArrayAdapter<String>(this, R.layout.list, suggestions);
AutoCompleteTextView  searchText = //findView...
searchText.setAdapter(suggestionAdapter);

hope this works.

OTHER TIPS

put the following line after the for loop.

suggestionAdapter.notifyDataSetChanged();

or put the following line in the first code.

suggestionAdapter.setNotifyOnChange(true)

More details setNotifyOnChange(boolean) , notifyDataSetChanged()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top