How to know, from addTextChangedListener, how the

2019-03-02 05:32发布

问题:

I have a screen with an EditText and a ListView. As you type into the EditText, an AsyncTask is launched (any existing AsyncTask is cancelled and tossed) to query locally for some data to display in the ListView. If the EditText is empty of text, all data is queried for and displayed. Essentially, the EditText is a filter of the data on the screen. This is easy to do. In the addTextChangedListener, simply cancel your previous AsyncTask, launch a new one, and pass to it s.toString().

When the user hits the backspace to erase some text in the EditText, I want the default behavior (where all data populates the list). This works with my current implementation of addTextChangedListener.

However, I also have an X button on the right side of my EditText. This is intended to clear out the EditText (EditText.setText("");). This is a special case. In this case, I do not want the query to happen. The problem is, addTextChangedListener does not seem intelligent enough to know this. It gets triggered because the text has changed and all data is queried for.

How do I prevent this?

回答1:

Use a boolean ignoreNextTextChangeKThxBye data member, setting/clearing that boolean as appropriate and using an if() statement to avoid doing the query.