1

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;
    }
}
Price
  • 2,683
  • 3
  • 17
  • 43
  • have you read Filterable and Filter ? http://developer.android.com/reference/android/widget/Filterable.html & http://developer.android.com/reference/android/widget/Filter.html – Haresh Chhelana Dec 01 '14 at 12:32
  • see my answer here on how to do it easily without implementing Filterable/Filter stuff – pskink Dec 01 '14 at 12:36
  • @pskink, I think you missed the link in your comment. – Price Dec 01 '14 at 12:39
  • @HareshChhelana, I read the reference but couldn't make much sense of the numerous methods and interfaces mentioned there. Would you know of any training guide on Filters and the Filterable interface? – Price Dec 01 '14 at 12:41
  • ooops, sorry http://stackoverflow.com/questions/19858843/how-to-dynamically-add-suggestions-to-autocompletetextview-with-preserving-chara – pskink Dec 01 '14 at 12:41

1 Answers1

0

->We can also achieve this with an EditText object along with a 'TextWatcher'

    EditText searchText = (EditText) findViewById(R.id.searchText);
    searchText.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable arg0) {
            // Do nothing
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // Do nothing
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s);
        }

Note: filter() would internally call 'performFiltering()' .Get the whole explanation and code from this blog: here

->AutoCompleteTextView does the job for us and abstracts away all coding complexity. If you have a look at it's code, you would com across the below part which calls performFiltering() once a minimum number of characters is typed in the input TextView.

void doAfterTextChanged() {
 //....Some code...
    if (enoughToFilter()) {
        if (mFilter != null) {
            mPopupCanBeUpdated = true;
            performFiltering(getText(), mLastKeyCode);
        }
    }
  //...Some code...
}
Mikki
  • 128
  • 2
  • 12