ArrayAdapter need to be clear even i am creating a new one

Posted by Roi on Stack Overflow See other posts from Stack Overflow or by Roi
Published on 2014-06-07T11:51:19Z Indexed on 2014/06/07 15:24 UTC
Read the original article Hit count: 403

Hello I'm having problems understanding how the ArrayAdapter works. My code is working but I dont know how.(http://amy-mac.com/images/2013/code_meme.jpg)

I have my activity, inside it i have 2 private classes.:

public class MainActivity extends Activity {
    ...
    private void SomePrivateMethod(){
        autoCompleteTextView.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, new ArrayList<String>(Arrays.asList(""))));
        autoCompleteTextView.addTextChangedListener(new MyTextWatcher());
    }
    ...

    private class MyTextWatcher implements TextWatcher {
        ...
    }
    private class SearchAddressTask extends AsyncTask<String, Void, String[]> {
        ...
    }
}

Now inside my textwatcher class i call the search address task:

@Override
public void afterTextChanged(Editable s) {
    new SearchAddressTask().execute(s.toString());
}

So far so good.

In my SearchAddressTask I do some stuff on doInBackground() that returns the right array.

On the onPostExecute() method i try to just modify the AutoCompleteTextView adapter to add the values from the array obtained in doInBackground() but the adapter cannot be modified:

NOT WORKING CODE:

protected void onPostExecute(String[] addressArray) {
    ArrayAdapter<String> adapter = (ArrayAdapter<String>) autoCompleteDestination.getAdapter();
    adapter.clear();
    adapter.addAll(new ArrayList<String>(Arrays.asList(addressArray)));
    adapter.notifyDataSetChanged();
    Log.d("SearchAddressTask", "adapter isEmpty : " + adapter.isEmpty()); // Returns true!!??!
}

I dont get why this is not working. Even if i run it on UI Thread...

I kept investigating, if i recreate the arrayAdapter, is working in the UI (Showing the suggestions), but i still need to clear the old adapter:

WORKING CODE:

protected void onPostExecute(String[] addressArray) {
    ArrayAdapter<String> adapter = (ArrayAdapter<String>) autoCompleteDestination.getAdapter();
    adapter.clear();
    autoCompleteDestination.setAdapter(new ArrayAdapter<String>(NewDestinationActivity.this,android.R.layout.simple_spinner_dropdown_item, new ArrayList<String>(Arrays.asList(addressArray))));
    //adapter.notifyDataSetChanged(); // no needed
    Log.d("SearchAddressTask", "adapter isEmpty : " + adapter.isEmpty()); // keeps returning true!!??!
}

So my question is, what is really happening with this ArrayAdapter? why I cannot modify it in my onPostExecute()? Why is working in the UI if i am recreating the adapter? and why i need to clear the old adapter then?

I dont know there are so many questions that I need some help in here!!

Thanks!!

© Stack Overflow or respective owner

Related posts about android

Related posts about android-asynctask