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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With