AutoCompleteTextView not populating with selected item

627 views Asked by At

I've done a fair bit of Google-fu but I cannot figure out what's wrong. Filtering works, the drop down list appears. But the AutoCompleteTextView doesn't populate with the selected item! Can anyone help?

I set a custom adapter to my AutoCompleteTextView that shows a custom layout.

actv = (AutoCompleteTextView) root.findViewById(R.id.actv);
actv.setAdapter(new MyCustomAutoCompleteAdapter(getActivity()));

Here are the important parts MyCustomAutoCompleteAdapter code:

public class MyCustomAutoCompleteAdapter extends ArrayAdapter<String> 
    implements Filterable {

    Context mContext;

    private ArrayList<String> resultList = new ArrayList<>();

    public MyCustomAutoCompleteAdapter(Context context) {
    // is this the correct way to super?
    super(context, R.layout.my_custom_layout);
    mContext = context;


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        return convertView;
    }

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

    @Override
    public String getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    // Retrieve the autocomplete results.
                    resultList = doPlacesSearchQuery(constraint.toString());

                    // Assign the data to the FilterResults
                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                }
                return filterResults;
            }

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

    private ArrayList<String> doPlacesSearchQuery(String query) {

        ArrayList<String> retList = new ArrayList<>();

        ... // do my API call here

        return retList;
    }
}
1

There are 1 answers

0
terencey On

I found my answer; I was indeed using the wrong super constructor.

public MyCustomAutoCompleteAdapter(Context context) {
    super(context, 
            R.layout.my_custom_layout, 
            R.id.id_of_textview_in_my_custom_layout);
    mContext = context;
}