Last active
December 23, 2015 15:18
-
-
Save mderazon/6654170 to your computer and use it in GitHub Desktop.
venue filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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