0

Working on an android app, which has a form with autocompletetextview with custom adapter, for which we have arraylist of around 1k entries. But with 1K data it is not working. With around 400 entries, it is working but filtering is slow. What can be done to handle this kind of large data set?

following is the code snippet for filtering:

     @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {
            suggestions.clear();

            for (ToMeet toMeet : tempItems) {
                if (toMeet.getNm().toLowerCase().contains(constraint.toString().toLowerCase()) ||
                        (toMeet.getAptNo() != null && toMeet.getAptNo().toLowerCase().toLowerCase().
                                contains(constraint.toString().toLowerCase()))) {

                    suggestions.add(toMeet);
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        if (results != null && results.count > 0) {
            ArrayList<ToMeet> filteredList = (ArrayList<ToMeet>) ((ArrayList<ToMeet>) results.values).clone();
            clear();
            for (ToMeet toMeet : filteredList) {
                add(toMeet);

            }
            notifyDataSetChanged();
        }
    }
};
alka aswal
  • 511
  • 1
  • 7
  • 22

1 Answers1

1

In your publishResults method you are calling the Adapter add method for each result. Every time the add method is called, the list is notified that it has been changed.

This could cause the slow behavior you are seeing.

Instead use the Adapter addAll method. The notification that the dataset has changed should be called for you by addAll unless you changed the default behavior with the setNotifyOnChange

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    clear();
    if (results != null && results.count > 0) {
        addAll(results)
    }
}

You could also optimize your performFiltering method to use a trie datastructure, but I don't think you should need to for 1,000 items.

ditkin
  • 6,774
  • 1
  • 35
  • 37