In this tutorial, Google has let the list adapter of an AutoCompleteTextView implement the Filterable interface to get auto-complete suggestions for places using the Google Places API as per the code given below.
Can someone please help me understand how this implementation of the Filterable interface work and when is the getFilter() method called?
In this same tutorial, Google has advised to wait for the user to pause typing before getting the AutoComplete suggestions. How can we achieve this in the below implementation? I saw some Stack Overflow posts that extend the AutoCompleteTextView to make the autocompletion wait, but is it also possible with the Filterable interface implementation?
Thanks for clarifying this for me!
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
autoCompView.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
…
}
private class PlacesAutoCompleteAdapter extends ArrayAdapter<String>
implements Filterable {
…
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}