Issue
What's the best way to "throttle" onQueryTextChange
so that my performSearch()
method is called only once every second instead of every time the user types?
public boolean onQueryTextChange(final String newText) {
if (newText.length() > 3) {
// throttle to call performSearch once every second
performSearch(nextText);
}
return false;
}
Solution
I ended up with a solution similar to below. That way it should fire once every half second.
public boolean onQueryTextChange(final String newText) {
if (newText.length() > 3) {
if (canRun) {
canRun = false;
handler.postDelayed(new Runnable() {
@Override
public void run() {
canRun = true;
handleLocationSearch(newText);
}
}, 500);
}
}
return false;
}
Answered By - aherrick
Answer Checked By - Mary Flores (JavaFixing Volunteer)