Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh ArrayAdapter dynamically

I have an Activity with an AutoComplete box on it. Whenever the text changes, I want to call a web service and populate the ArrayAdapter with the new String[] returned. This part all works great, except the list in the UI isn't being refreshed when there are all new values in String[] schools. The original list populated in my onCreate always remains.

I read somewhere I needed to update it on the same thread the UI is running on, so I tried the Runnable listed in my code below. But that, as well as just updating my class variable schools does not work alongside notifyOnDataSetChange()

Where am I going wrong?

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    schools = SchoolProxy.search("Co");
    autoCompleteAdapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, schools);
    autoComplete = (AutoCompleteTextView)  findViewById(R.id.edit);
    autoComplete.addTextChangedListener(textChecker);
    autoComplete.setAdapter(autoCompleteAdapter);
}
    ...
    ....
    ...

final TextWatcher textChecker = new TextWatcher() {
    public void afterTextChanged(Editable s) {}

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

    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {
     schools = SchoolProxy.search(s.toString());
     runOnUiThread(updateAdapter);
    }
};

private Runnable updateAdapter = new Runnable() {
  public void run() {
         autoCompleteAdapter.notifyDataSetChanged();
        }

};

As a less important side note, if each item in schools has a name \n city,state. Is it possible to have the city & state on a second line within the auto drop down box? Just for formatting purposes. Would look cleaner if I could.

Thank you!

like image 455
Nathan Avatar asked Dec 28 '09 03:12

Nathan


People also ask

How do I refresh ArrayAdapter?

You can modify existing adapter data and call notifyDataSetChanged(). In your case you should call listView. setAdapter(adapter) in onClick method.

How can you update a ListView dynamically?

This example demonstrates how do I dynamically update a ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is notify data set changed?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.


1 Answers

Have you tried the setNotifyOnChange(true)? It is supposed to properly do what you are doing manually whenever you use methods that change the list (add(T), insert(T, int), remove(T), clear()). Perhaps you have to modify the array through these methods on the ArrayAdapter?

I'm not sure if the ArrayAdapter is actually holding the reference to schools or just copied the contents while constructing the ArrayAdapter. Maybe you can take a look at the AOSP code to see what they're doing in that constructor.

like image 98
GrkEngineer Avatar answered Sep 19 '22 01:09

GrkEngineer