AutoSearchTextView dropdown flickers when data changes

1.1k Views Asked by At

I have an AutoSearchTextView in the actionbar. When I filter the data the dropdown hides and then shows very quickly. I really would prefer the effect on the play store, where you filter results, the dropdown is always visible but the content in the dropdown changes.

This is my filter code in my Adapter, run history query, gets the history and sets the list items.

@Override
public Filter getFilter() {
    if (filter == null) {
        filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                ResortFilterResult result = new ResortFilterResult();
                String substr = constraint.toString().toLowerCase();
                if (constraint != null) {
                    if (constraint.length() > 0) {
                        result.setHistory(runHistoryQuery(substr));
                    }
                    if (constraint.length() >= 3) {
                        LCResort[] resorts = searchResortWithQuery(substr);
                        result.setQueryResults(Arrays.asList(resorts));
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = result;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                ResortFilterResult result = (ResortFilterResult)results.values;
                history = result.getHistory();
                resorts = result.getQueryResults();
                notifyDataSetChanged();

            }
        };
    }
    return filter;
}

EDIT: I am one step closer - It turns out dismiss dropdown on the TextView is being called every time I type a new character!

2

There are 2 best solutions below

4
On

please don't reinvent the wheel, use existing Filter instead of creating your own one

and the easiest is to use FilterQueryProvider together with e.g. SimpleCursorAdapter or when extending CursorAdapter just override runQueryOnBackgroundThread(), using that you will avoid errors like you have now

EDIT:

in your case if you dont need a custom Adapter use a SimpleCursorAdapter with custom FilterQueryProvider overriding runQuery() you can return all the data you need (see my answer here how i query wikipedia: How to dynamically add suggestions to autocompletetextview with preserving character status), if you REALLY need a custom Adapter extend CursorAdapter and do exactly the same in runQueryOnBackgroundThread() what you did in FilterQueryProvider.runQuery(), also when your data comes from more then one source use a MergeCursor but its perfectly ok to just use one MatrixCursor

0
On

Okay, It turns out the answer was quite simple, sometimes the best way it to just check the source code :)

AutoSearchTextView has a method public void onFilterComplete(int count) which in turn calls private void updateDropDownForFilter(int count), if the count is zero the dropdown will be hidden.

You must set count on your FilterResults object returned from your Filter protected FilterResults performFiltering(CharSequence constraint) method.

filterResults.count = result.getHistory().size() + result.getQueryResults().size();

solved the issue.