Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic AutocompleteTextView with ArrayAdapter and TextWatcher

I'm trying to update the list of an AutocompleteTextView dynamically using and ArrayAdapter. In order to update this View I use a TextWatcher to monitor any changes that may occur in the AutocompleteTextView.

The problem is that the list isn't updating at all and I can't understand why. I've been looking for something like that on internet and I've found couple of different approaches but still I can't understand why this one, that should be the simplest one, isn't working. Any explaination would be much appreciated.

Target AVD: Google APIs level 10, Android 2.3.3

Here is the simplified code:

public class AutocompleteActivity extends Activity implements TextWatcher {
    ArrayAdapter<String> adapter = null;
    AutoCompleteTextView acTextView = null;
    ArrayList<String> addresses = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocompleteview);

        acTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete_address);
        adapter = new ArrayAdapter<String>(this, R.layout.listitem, addresses);
        adapter.setNotifyOnChange(true);
        acTextView.addTextChangedListener(this);
        acTextView.setAdapter(adapter);
    }

    @Override
    public void onTextChanged(CharSequence text, int start, int before, int after) {
        try {
            adapter.add("test");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
like image 685
gioski Avatar asked Oct 24 '22 00:10

gioski


1 Answers

In your source code, TextWatcher is supplied to the AutoCompleteTextView, this is the real problem.

If you look at AutoCompleteTextView source code, you will find AutoCompleteTextView has its own TextWatcher, named "MyWatcher". Becauseof this, AutoCompleteTextView can not repond to your typing action.

like image 90
cmoaciopm Avatar answered Oct 27 '22 11:10

cmoaciopm