AutoCompleteTextView adapter returning old values

Viewed 2327

I implemented an AutoCompleteTextView where the data is updated from the server every time the user enters a new text input. It works well. However, every time I enter a new query, the previous results are displayed until the new result set is updated.

My guess is that it displays the currently queried results until the backend responds with the new search results. Is there a way to clear the current results?

Activity code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_test);

    actv = (AutoCompleteTextView) findViewById(R.id.actv);
    actv.setThreshold(1);
    actv.setAdapter(new SearchSuggestionsAdapter(this, actv.getText().toString()));

Custom adapter:

public class SearchSuggestionsAdapter extends ArrayAdapter<String> {

    List<String> types = new ArrayList<>();
    protected static final String TAG = "SuggestionAdapter";
    private List<String> suggestions;
    private Context context;

    public SearchSuggestionsAdapter(Activity context, String nameFilter) {
        super(context, android.R.layout.simple_dropdown_item_1line);
        suggestions = new ArrayList<String>();
        this.context = context;
    }

    @Override
    public int getCount() {
        return suggestions.size();
    }

    @Override
    public String getItem(int index) {
        return suggestions.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();

                if (constraint != null) {

                    //Get new results from backend
                    //searchItemsFromServer is the method that returns the data
                    //new data is successfully sent. no problem there
                    List<String> new_suggestions = searchItemsFromServer(constraint.toString());
                    suggestions.clear();
                    for (int i = 0; i < new_suggestions.size(); i++) {
                        suggestions.add(new_suggestions.get(i));
                    }

                    filterResults.values = suggestions;
                    filterResults.count = suggestions.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence contraint,
                                          FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return myFilter;
    }
}

Thank you in advance!

3 Answers

Change your code to your activity to this

SearchSuggestionsAdapter searchSuggestionsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_test);

    searchSuggestionsAdapter = new SearchSuggestionsAdapter(this, actv.getText().toString());
    actv = (AutoCompleteTextView) findViewById(R.id.actv);
    actv.setThreshold(1);
    actv.setAdapter(searchSuggestionsAdapter);
    actv.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            if(!hasFocus){
              searchSuggestionsAdapter.clear();
            }
        }
    });
}

Add the following code to your SearchSuggestionsAdapter

/**
 * Create list and notify recycler view
 */
public void clear() {
    if (suggestions != null) {
        suggestions.clear();
        notifyDataSetChanged();
    }
}
Related