Sunday, 22 July 2012

AutoCompleteTextView with SimpleAdapter


public class StopSimpleAdapter extends SimpleAdapter implements Filterable  {

 private ArrayList‹HashMap‹String, String›› mAllStops;
 private ArrayList‹HashMap‹String, String›› mStopsShown;
public StopSimpleAdapter(Context context, List stops, int resource, String[] from, int[] to) { super(context, stops, resource, from, to); mStopsShown = (ArrayList‹HashMap‹String, String››) stops; mAllStops = (ArrayList‹HashMap‹String, String››) mStopsShown.clone(); } @Override public Filter getFilter() { Filter nameFilter = new Filter(){ @Override public String convertResultToString(Object resultValue) { return ((HashMap‹String, String›)(resultValue)).get(Stop.NAME); } @Override protected FilterResults performFiltering(CharSequence constraint) { if(constraint != null) { ArrayList‹HashMap‹String, String›› tmpAllStops = mAllStops; ArrayList‹HashMap‹String, String›› tmpStopsShown = mStopsShown; tmpStopsShown.clear(); for(int i = 0; i ‹ tmpAllStops.size(); i++) { if(tmpAllStops.get(i).get(Stop.NAME).toLowerCase().startsWith(constraint.toString().toLowerCase())) { tmpStopsShown.add(tmpAllStops.get(i)); } } FilterResults filterResults = new FilterResults(); filterResults.values = tmpStopsShown; filterResults.count = tmpStopsShown.size(); return filterResults; } else { return new FilterResults(); } } @Override protected void publishResults(CharSequence constraint, FilterResults results) { if(results != null && results.count › 0) { notifyDataSetChanged(); } }}; return nameFilter; } }

No comments:

Post a Comment