Question

I'm having trouble with an autocompletetextview. When I first start the app the autocompletetextview work's as expected, I can type in 'm' and get a list of all restaurants which start with 'm'...

Autocompletetextview with

Then when I type more, 'mark', the list updates as expected...

Autocompletetextview with

However the problem happens when I delete the text and start over with a different letter, the autocompletetextview no longer updates with other letters...

Autocompletetextview with

Any idea why clearing the text doesn't allow me to "start over" with the filtering? Here is my ArrayAdapter code...

 class AutoLocationAdapter extends ArrayAdapter<Location> implements Filterable{
    private ArrayList<Location> locations;
    private ArrayList<Location> allLocations;
    private Filter filter;

    public AutoLocationAdapter(Context context, int layout, ArrayList<Location> locations) {
        super(context, layout , locations);
        this.locations = locations;
        this.allLocations = locations;
    }

    @Override
    public int getCount() {
        return locations.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.fragment_locations_auto_row, null);
        }

        Location l = locations.get(position);

        if (l != null) {

            TextView name = (TextView) v.findViewById(R.id.frag_location_auto_name);
            TextView town = (TextView) v.findViewById(R.id.frag_location_auto_town);

            if (name != null){
                name.setText(l.getName());
            }
            if (town != null){
                town.setText(l.getTown());
            }
        }

        return v;
    }

    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new LocationFilter();
        }
        return filter;
    }

    private class LocationFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults results = new FilterResults();
            if (charSequence == null ||charSequence.length()==0) {
                results.values = locations;
                results.count = locations.size();
                Log.v("!!!", "cleared filter");
                locations = allLocations;
                notifyDataSetChanged();
            }
            else {
                ArrayList<Location> nLocations = new ArrayList<Location>();
                for(Location l : locations){
                    if(l.getName().toUpperCase().startsWith(charSequence.toString().toUpperCase())){
                        nLocations.add(l);
                    }
                }
                results.values = nLocations;
                results.count = nLocations.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            if (filterResults.count==0) {
                notifyDataSetInvalidated();
            }
            else{
                locations = (ArrayList<Location>) filterResults.values;
                notifyDataSetChanged();
            }
        }
    }
Was it helpful?

Solution

if (charSequence == null ||charSequence.length()==0) {
  results.values = locations;
  results.count = locations.size();

  Log.v("!!!", "cleared filter");
  locations = allLocations;
  notifyDataSetChanged();
}

So, basically when you've already filtered, you're saying here that results.values = locations which corresponds to the previous filtering. So if the new filtering pattern don't match the set that you previously filtered, it won't show anything, what you're basically experiencing. You have to first set locations = allLocations and then set the results.* params:

if (charSequence == null ||charSequence.length()==0) {
  Log.v("!!!", "cleared filter");
  locations = allLocations;
  notifyDataSetChanged();

  results.values = locations;
  results.count = locations.size();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top