Skip to content

Instantly share code, notes, and snippets.

@mderazon
Last active December 23, 2015 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mderazon/6654170 to your computer and use it in GitHub Desktop.
Save mderazon/6654170 to your computer and use it in GitHub Desktop.
venue filter
private class VenueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<JSONObject> list = new ArrayList<JSONObject>(venues);
FilterResults result = new FilterResults();
String substr = constraint.toString().toLowerCase();
// if no constraint is given, return the whole list
if (substr == null || substr.length() == 0) {
result.values = list;
result.count = list.size();
} else {
// iterate over the list of venues and find if the venue matches the constraint. if it does, add to the result list
final ArrayList<JSONObject> retList = new ArrayList<JSONObject>();
for (JSONObject venue : list) {
try {
if (venue.getString("name").toLowerCase().contains(constraint) || venue.getString("address").toLowerCase().contains(constraint) ||
{
retList.add(venue);
}
} catch (JSONException e) {
Log.i(Consts.TAG, e.getMessage());
}
}
result.values = retList;
result.count = retList.size();
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// we clear the adapter and then pupulate it with the new results
searchAdapter.clear();
if (results.count > 0) {
for (JSONObject o : (ArrayList<JSONObject>) results.values) {
searchAdapter.add(o);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment