Saturday, May 15, 2010

[android-developers] AutoCompleteTextView and setText()

I found a thread with this subject from about 6 months ago with no resolution, so I'll start it up again. 

This is an excerpt from the TextView API: 

public void addTextChangedListener (TextWatcher watcher)

Since: API Level 1
Adds a TextWatcher to the list of those whose methods are called whenever this TextView's text changes.
In 1.0, the afterTextChanged(Editable) method was erroneously not called after setText(char[], int, int) calls. Now, doingsetText(char[], int, int) if there are any text changed listeners forces the buffer type to Editable if it would not otherwise be and does call this method.
This seems to make sense for a TextView.  Then, when you look at AutoCompleteTextView, I found that it will display the auto complete view every time afterTextChanged is called: 

    private class MyWatcher implements TextWatcher {
        public void afterTextChanged(Editable s) {
            doAfterTextChanged();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            doBeforeTextChanged();
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    }

My only choice seems to be to extend AutoCompleteTextView and change the behavior.  I am not able to override "doAfterTextChanged()", nor am I able to keep it from showing up.

The best solution I can find is to override addTextChangedListener, and ignore the TextWatcher the AutoCompleteTextView wants to send, and substitute my own TextWatcher with a more flexible behavior.  

Any comments?   Is there a much easier solution I've simply missed?

-Matthew

No comments:

Post a Comment