2

I write following application:

  • there is an AutoCompleteTextView field
  • as Adapter I'm using ArrayAdapter with ListArray
  • the ListArray consists of some constant string item and one item, which will be changed dynamically everytime user typed something in the field

I took TextChangedListener to update this last list item. But it seems, that update occurs only once.

I add a bit code of me. May be somebody can show me, what did i do wrong.

public class HelloListView extends Activity 
{
    List<String> countryList = null;    
    AutoCompleteTextView textView = null;   
    ArrayAdapter adapter = null;

    static String[] COUNTRIES = new String[] 
    {
          "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
          "Yemen", "Yugoslavia", "Zambia", "Zimbabwe", ""
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

        countryList = Arrays.asList(COUNTRIES);

        textView = (AutoCompleteTextView) findViewById(R.id.edit);
        adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, countryList);
        adapter.notifyDataSetChanged();        
        textView.setAdapter(adapter);
        textView.setThreshold(1);

        textView.addTextChangedListener(new TextWatcher() 
        {

            public void onTextChanged(CharSequence s, int start, int before, int count) 
            {               
                countryList.set(countryList.size()-1, "User input:" + textView.getText());                
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) 
            {               
            }

            public void afterTextChanged(Editable s) 
            {
            }
        });

        new Thread() 
        {
            public void run() 
            {
                // Do a bunch of slow network stuff.
                update();
            }
        }.start();        
    }

    private void update() 
    {
        runOnUiThread(new Runnable() 
        {
            public void run() 
            {
                adapter.notifyDataSetChanged();
            }
        });
    }
}
RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
Tima
  • 12,765
  • 23
  • 82
  • 125
  • Not sure why what you have doesn't work, but did you consider using the AsyncTask for doing your network stuff? It give you an easy way to do work on another thread, then do a bit of work on the UI thread when its doen. http://developer.android.com/reference/android/os/AsyncTask.html – Cheryl Simon Sep 08 '10 at 17:48
  • But if you look at my example, there are no network stuff at all. I have ArrayList's Data already in code. – Tima Sep 09 '10 at 06:48
  • Tryed your suggestion. It doesn't work – Tima Sep 11 '10 at 09:46
  • Please see my answer: https://stackoverflow.com/a/51808479/5128831 – Homayoon Ahmadi Aug 12 '18 at 11:43

2 Answers2

12

Do not modify the ArrayList. Modify the ArrayAdapter, using add(), insert(), and remove(). You do not need to worry about notifyDataSetChanged().

Also, I agree with Mayra -- consider using AsyncTask instead of your thread and runOnUiThread() combination.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I've got a suggestion to use runOnUIThread by stackoverflow. add, insert and remove operation are not very nice in my case. I'd like to have only one item, i will change. – Tima Sep 08 '10 at 18:50
2

In the function onTextChanged(), create a new adapter and attach it, then it'll work:

countryList.set(countryList.size()-1, "User input:" + textView.getText());      
adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, countryList);
textView.setAdapter(adapter);

Seems it isn't a perfect one, but it works!

sth
  • 222,467
  • 53
  • 283
  • 367
jowett
  • 774
  • 5
  • 10