I have a search field that I want to autocomplete with data that I collect from two different places: a local database (sqlite), and a remote search API. I’d like to show the local options immediately, and then after a pause (so that I’m not making API requests on every keypress), start the API request.
The problem comes when the API request returns. How do I add those entries to the (already visible) dropdown for the AutoCompleteTextArea? Right now, I have a class that extends BaseAdapter:
private class MyAutoCompleteAdapter extends BaseAdapter implements Filterable {
private List<String> resultList;
private MySearchFilter filter;
public BeerAutoCompleteAdapter() {
super();
results = new ArrayList<String>();
filter = new MySearchFilter();
}
// getView, getCount, getItem overridden
private void add(String newResult) {
results.add(newResult);
notifyDataSetChanged();
}
private class MySearchFilter extends Filter {
// performFiltering and publishResults overridden
}
}
Everything works 90%. When I call add(newResult), the new result gets added to the bottom of the dropdown. Unfortunately, the height of the dropdown doesn’t change. If there were two results before the API request, and one comes back from the API, all that happens is that I get a scrollbar on the dropdown. As soon as I scroll, the UI decides “oh, yeah, there’s room for that one too” and shows it.
I’ve tried forceLayout() and requestLayout() on my AutoCompleteTextArea, as well as notifyDataSetInvalidated(). No dice.
I figured it out. I was looking through the
AutoCompleteTextAreasource code and I noticed thatshowDropDown()had the logic for calculating height and width, and that it does that even if the dropdown is already visible. I tried it (in addition tonotifyDataSetChanged()) and it worked.