0

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.

rahul
  • 6,447
  • 3
  • 31
  • 42

2 Answers2

0

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()

satheesh
  • 187
  • 1
  • 4
  • 14
0

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.

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • but this is quite an obvious logic. If this is what you have already done. please do post the complete activity code or, atleast all the code related to the AutoCompleteTextView and the adapter. Just to see the code flow – Archie.bpgc Jul 29 '12 at 16:28
  • Thx Archie!!! You saved me. And yes this is an obvious code. In my early apps, I never set the arrayList dynamically. I used to initialize the list at the start of the code and then set that to the adapter. In this app, I have to pull some data off the internet and then populate the list. – rahul Jul 29 '12 at 16:52