LaVOZs

The World’s Largest Online Community for Developers

'; android - AutoCompleteTextView custom filter results are ignored - LavOzs.Com

I've created a custom adapter for AutoCompleteTextView.

package ...

import ...

public class CardSuggestionAdapter extends ArrayAdapter<String> implements Filterable {

    private final LayoutInflater mInflater;
    private Activity context;
    private int mFieldId, mResource;

    CardSuggestionAdapter(Activity context, String[] names) {
        super(context, R.layout.card_suggestion, R.id.name_label, names);
        this.context = context;
        this.mFieldId = R.id.name_label;
        this.mResource = R.layout.card_suggestion;
        this.mInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return createViewFromResource(mInflater, position, convertView, parent, mResource);
    }

    @NonNull
    @Override
    public Filter getFilter() {
        return new CardsFilter();
    }

    private View createViewFromResource(@NonNull LayoutInflater inflater, int position, @Nullable
            View convertView, @NonNull ViewGroup parent, int resource) {
        View view = convertView;
        final TextView text;

        ViewHolder viewHolder;
        if (convertView == null) {
            view = inflater.inflate(resource, parent, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        text = view.findViewById(mFieldId);
        if (text == null) {
            throw new RuntimeException("Failed to find view with ID "
                    + context.getResources().getResourceName(mFieldId)
                    + " in item layout");
        }

        final String item = getItem(position);
        viewHolder.name.setText(item);
        viewHolder.name.setTypeface(Typeface.createFromAsset(
                MarsPDA.assetManager, "fonts/prototype.ttf"));

        return view;
    }

    class ViewHolder {

        TextView name;

        ViewHolder(View v) {
            name = v.findViewById(R.id.name_label);
        }
    }

    private class CardsFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                final List<String> suggestions = new ArrayList<>();
                final FilterResults fs = new FilterResults();
                return fs;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}

This implementation of performFiltering obviously makes that no suggestion is displayed.

I tried copying performFiltering method from original ArrayAdapter class as well as looking into some other implementations of Filter found on this forum, but even when I add actually literally anything to suggestions list and set fs.values = suggestions; and fs.count = suggestions.size();, the adapter shows all members of the names array passed into the constructor. Why is that happening?

I think the problem is in manging list, you can try below changes Declare both list original and filter and then initialise it in constructor.

Override get count method.

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

Make changes in filter class as below:

private class CardsFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
            List<String> FilteredArrList = new ArrayList<String>();
            if (constraint == null || constraint.length() == 0) {

                // set the Original result to return
                results.count = namesOriginal.size();
                results.values = namesOriginal;
            } else {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < namesOriginal.size(); i++) {
                    String data = namesOriginal.get(i);
                    if (data.toLowerCase().startsWith(constraint.toString())) {
                        FilteredArrList.add(data);
                    }
                }
                // set the Filtered result to return
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }
            return results;

        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null && results.count > 0) {
                names= (ArrayList<String>) results.values;
                notifyDataSetChanged();
            } else {
                names.addAll(namesOriginal);
                notifyDataSetInvalidated();
            }
        }
    }

Hope this will help.

Related
How to hide the title bar for an Activity in XML with existing custom theme
list comprehension vs. lambda + filter
Filter list view from edit text
Saving ToggleButton state in ListView by using SharedPreferences