简体   繁体   中英

AutoCompleteTextView not populating with selected item

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;
    }
}

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;
}

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