简体   繁体   中英

AutoCompleteTextView Filter doesn't show the filtered list

I have a problem with my filter in my custom Adapter for my AutoCompleteTextView. It doesn't want to show the filtered list. I know that the filter filters my list because for example I have "Paris/FRANCE" in my list and when I type it in the AutoCompleteTextView, the list is still shown but it continues to show the complete list and not the filtered list. If I type "Pary", the list disappear after typing the "Y" letter. That's why I know that the filter works.

Here is my code:

public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable
{
    private ArrayList<String> data;

    public AutoCompleteAdapter(Context context, int resource, ArrayList<String> objects) 
    {
        super(context, resource, objects);
        data = objects;
    }



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

        if (v == null) 
        {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.item, null);
        }

        String s = getItem(position);

        if (s != null) 
        {
            TextView city = (TextView)v.findViewById(R.id.city);
            TextView country = (TextView)v.findViewById(R.id.country);

            String[] split = s.split("/");
            city.setText(split[0]);
            country.setText(split[1]);
        }

        return v;

    }

    @Override
    public Filter getFilter()
    {
        return new Filter()
        {

            @Override
            protected FilterResults performFiltering(CharSequence prefix)
            {
                FilterResults fr = new FilterResults();
                if(prefix != null)
                {
                    ArrayList<String> filtered = new ArrayList<String>();
                    for(String s : data)
                    {
                        if(s.toLowerCase().contains(prefix.toString().toLowerCase()))
                        {
                            filtered.add(s); 
                        }
                    }

                    fr.values = filtered;
                    fr.count = filtered.size();
                }
                return fr;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence contraint, FilterResults results) 
            {
                data = (ArrayList<String>)(results.values);

                if(results != null && results.count > 0) 
                {
                    notifyDataSetChanged();
                }
                else 
                {
                    notifyDataSetInvalidated();
                }
            }

        };
    }
    }

Please forgive me for my bad english and I hope you will help me. Thanks

You never filter the base class, so methods that you don't overwrite behave the same way they would, if no filtering was done.

Eg getCount() still uses the old list, since you replace the reference to the list in the data field, but not the reference in the ArrayAdapter base class.

If you modify data instead of replacing it, it should work:

Replace

data = (ArrayList<String>)(results.values);

with

data.clear();
data.addAll((ArrayList<String>)results.values);

However you may want to keep a backup of the original list somewhere, in case someone deletes letters from the string you use to filter (and therefore filtering of the filtered list would no longer produce the same result as filtering the original list with the current filter directly).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM