Reputation: 1266
I have a problem when I'm trying to Filter results in AutoCompleteTextView from LogCat I know that filtering is performed correct but it's not refreshing the view :/ Did I forget about something any suggestions or help?
Here is source code of filter.
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.i(TAG, "Perform filtering with constraint: " + constraint.toString());
List<String> resultsSuggestions = new ArrayList<String>();
Log.i(TAG, "COUNT: " + getCount());
for (int i = 0; i < getCount(); i++) {
if(getItem(i).getSuggestionValue().startsWith(constraint.toString())){
Log.i(TAG, "ADDED");
resultsSuggestions.add(getItem(i).getSuggestionValue());
}
}
FilterResults results = new FilterResults();
results.values = resultsSuggestions;
results.count = resultsSuggestions.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
Upvotes: 1
Views: 3168
Reputation: 11
Another Update - On inputting and removing all the characters in search textbox very quickly crashes the application on newValues.size() or newValues.get(i) as newValues might be null. So, here's the code you should use:
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
ArrayList<Suggestions> newValues = (ArrayList<Suggestions>) results.values;
if(newValues !=null) {
for (int i = 0; i < newValues.size(); i++) {
add(newValues.get(i));
}
if(results.count>0){
notifyDataSetChanged();
} else{
notifyDataSetInvalidated();
}
}
Upvotes: 1
Reputation: 1266
The missing part is that I need to set the new values from filter so I just simply changed the
publushResults();
and now it's working. The correct code bellow.
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
ArrayList<Suggestions> newValues = (ArrayList<Suggestions>) results.values;
for (int i = 0; i < newValues.size(); i++) {
add(newValues.get(i));
}
if(results.count>0){
notifyDataSetChanged();
} else{
notifyDataSetInvalidated();
}
}
Upvotes: 2