I'm dealing with the InstantSearch feature on my Android App and the result I'd like to achieve is the one in the following picture.
Everything works properly except for one thing. The ArrayList I'm using is BIG (it contains almost 400k words). Hence, I have performance issues while showing my results on the ListView (it lags a lot).
I'm pretty sure it's caused by the length of my Wordlist because, reducing its number of words, everything is smooth as butter.
Here's my filtering code in the Adapter Class:
private class SearchResultsFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<String> found = new ArrayList<>();
if (constraint != null) {
for (String word : MainActivity.WordList) {
if (word.startsWith(constraint.toString().toLowerCase())) {
found.add(word);
}
}
}
filteredList = found;
filterResults.values = found;
filterResults.count = found.size();
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
if (filterResults.count > 0) {
Log.println(Log.INFO, "Results", "FOUND");
results.clear();
results.addAll((ArrayList<String>) filterResults.values);
notifyDataSetChanged();
} else {
Log.println(Log.INFO, "Results", "-");
results.clear();
notifyDataSetInvalidated();
}
}
}
That's how I load my ArrayList in my MainActivity:
public static void loadDictionary(Activity activity) {
//loading wordslist from file.
BufferedReader line_reader = new BufferedReader(new InputStreamReader(activity.getResources().openRawResource(R.raw.wordlist)));
String line;
try {
while ((line = line_reader.readLine()) != null) {
WordList.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Collections.sort(WordList);
Thank you everyone,
Have a nice day.