How to add a custom adapter to an AutoCompleteTextView

autocompletetextview with custom adapter kotlin
autocompletetextview custom adapter filter example
xamarin android autocompletetextview custom adapter
material autocompletetextview
autocompletetextview with arraylist in android
android autocompletetextview custom adapter github
autocompletetextview with listview in android
autocompletetextview update adapter

Is there any simple way to set a 2 TextView dropdown to an AutoCompleteTextView.

There is android.R.layout.two_line_list_item Which I couldn't find any examples how to use.

So, I tried this:

public class TwoLineDropdownAdapter extends BaseAdapter {

    private LayoutInflater mInflater = null;
    private Activity activity;
    public ArrayList<TwoLineDropDown> values = new ArrayList<TwoLineDropDown>();

    public TwoLineDropdownAdapter(Activity a, ArrayList<TwoLineDropDown> items) {

        values = items;
        activity = a;
        mInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {

        return values.size();
    }

    public TwoLineDropDown getItem(int position) {

        return values.get(position);
    }

    public long getItemId(int position) {

        return position;
    }

    public static class ViewHolder {

        public TextView title;
        public TextView description;
    }

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

        ViewHolder holder;

        if (convertView == null) {

            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.dropdown_text_twoline,
                    parent, false);
            holder.title = (TextView) convertView
                    .findViewById(R.id.text1);
            holder.description = (TextView) convertView
                    .findViewById(R.id.text2);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }

    public void add(TwoLineDropDown ei) {

        values.add(ei);
    }
}

But I face a problem here:

TwoLineDropdownAdapter AutoCompleteAdapter = new TwoLineDropdownAdapter(this, items);
myAutoComplete.setAdapter(AutoCompleteAdapter);

while setting the Adapter it says:

Bound mismatch: The generic method setAdapter(T) of type AutoCompleteTextView is not applicable for the arguments (TwoLineDropdownAdapter). The inferred type TwoLineDropdownAdapter is not a valid substitute for the bounded parameter

How to solve this?

Thank You

According to the documentation, the inferred type of setAdapter in AutoCompleteTextView is :

<T extends ListAdapter & Filterable> void setAdapter(T adapter)

Your adapter must be a ListAdapter (which BaseAdapter is, so far so good) and a Filterable, which BaseAdapter is not, nor is your Adapter implementation. I would extend an ArrayAdapter, which is Filterable, not to mention is would simplify your implementation (some of your methods duplicate methods of ArrayAdapter for the same result) :

public class TwoLineDropdownAdapter extends ArrayAdapter<TwoLineDropDown> {

    private LayoutInflater mInflater = null;
    private Activity activity;

    public TwoLineDropdownAdapter(Activity a, ArrayList<TwoLineDropDown> items) {
        super(a, 0, items);
        activity = a;
        mInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder {

        public TextView title;
        public TextView description;
    }

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

        ViewHolder holder;

        if (convertView == null) {

            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.dropdown_text_twoline,
                    parent, false);
            holder.title = (TextView) convertView
                    .findViewById(R.id.text1);
            holder.description = (TextView) convertView
                    .findViewById(R.id.text2);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }
}

AutoCompleteTextView with Custom Adapter Part 1, are going to build an AutoCompleteTextView with a custom adapter, We will also create Duration: 9:06 Posted: Mar 31, 2018 Thank you! That was really helpful! I had a problem - dropdown list in my AutoCompleteTextView didn't show up and getView method of my custom adapter hadn't been called. All I needed to do was adding getFilter method as on this example – Victor Lyan Feb 18 '16 at 17:12

Here Code is working for me,

Set This adapter to autocompletetextview

AutoCompleteTextView etProductSearch = (AutoCompleteTextView)getView().findViewById(R.id.edtSearchBoxTakeOrder);
ProductSearchAdapter adapter = new ProductSearchAdapter(getActivity(), android.R.layout.simple_dropdown_item_1line, productList);
etProductSearch.setAdapter(adapter );

ProductSearchAdapter class

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

public class ProductSearchAdapter extends ArrayAdapter<ProductDataModel> {
    private ArrayList<ProductDataModel> items;
    private ArrayList<ProductDataModel> itemsAll;
    private ArrayList<ProductDataModel> suggestions;
    private int viewResourceId;

    @SuppressWarnings("unchecked")
    public ProductSearchAdapter(Context context, int viewResourceId,
            ArrayList<ProductDataModel> items) {
        super(context, viewResourceId, items);
        this.items = items;
        this.itemsAll = (ArrayList<ProductDataModel>) items.clone();
        this.suggestions = new ArrayList<ProductDataModel>();
        this.viewResourceId = viewResourceId;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(viewResourceId, null);
        }
        ProductDataModel product = items.get(position);
        if (product != null) {
              TextView productLabel = (TextView)  v.findViewById(android.R.id.text1);
            if (productLabel != null) {
                productLabel.setText(product.getProductName());
            }
        }
        return v;
    }

    @Override
    public Filter getFilter() {
        return nameFilter;
    }

    Filter nameFilter = new Filter() {
        public String convertResultToString(Object resultValue) {
            String str = ((ProductDataModel) (resultValue)).getProductName();
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();
                for (ProductDataModel product : itemsAll) {
                    if (product.getProductName().toLowerCase()
                            .startsWith(constraint.toString().toLowerCase())) {
                        suggestions.add(product);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            @SuppressWarnings("unchecked")
            ArrayList<ProductDataModel> filteredList = (ArrayList<ProductDataModel>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (ProductDataModel c : filteredList) {
                    add(c);
                }
                notifyDataSetChanged();
            }
        }
    };

}

Android AutocompleteTextView with Custom Adapter Example, This is where the custom ArrayAdapter will come to the scene. Using this post, you will be able to customize the look and feel of your AutocompleteTextView� Questions: I’m stuck in a very similar situation as this: Android: Using item selected in AutoCompleteTextView to populate another field After hours of reading through so many posts & answers at SO I’ve managed to reach the point where the AutoCompleteTextView is populating list of customers (The custom object / POJO that I need) using

Converted Dwivedi Ji's answer to Kotlin. I had some issue with Android Studio's auto convert. Thus, spent some time to make it work.

Now it is working. In case anyone needs it (in my case, I am filtering street names):

class StreetsAdapter( private val mContext: Context,
                      private val viewResourceId: Int,
                      private val items: ArrayList<Street>) : ArrayAdapter<Street?>(mContext, viewResourceId, items.toList()) {

    private val itemsAll = items.clone() as ArrayList<Street>
    private var suggestions = ArrayList<Street>()

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var v: View? = convertView
        if (v == null) {
            val vi = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            v = vi.inflate(viewResourceId, null)
        }
        val street: Street? = items[position]
        if (street != null) {
            val streetTitle = v?.findViewById(R.id.tvStreetTitle) as TextView?
            streetTitle?.text = street.title
        }
        return v!!
    }

    override fun getFilter(): Filter {
        return nameFilter
    }

    private var nameFilter: Filter = object : Filter() {
        override fun convertResultToString(resultValue: Any): String {
            return (resultValue as Street).title
        }

        override fun performFiltering(constraint: CharSequence?): FilterResults {
            return if (constraint != null) {
                suggestions.clear()
                for (street in itemsAll) {
                    if (street.title.toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                        suggestions.add(street)
                    }
                }
                val filterResults = FilterResults()
                filterResults.values = suggestions
                filterResults.count = suggestions.size
                filterResults
            } else {
                FilterResults()
            }
        }

        override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
            val filteredList =  results?.values as ArrayList<Street>?

            if (results != null && results.count > 0) {
                clear()
                for (c: Street in filteredList ?: listOf<Street>()) {
                    add(c)
                }
                notifyDataSetChanged()
            }
        }
    }
}

And set your adapter:

val adapter = StreetsAdapter(this,
       R.layout.item_street, //Your layout. Make sure it has [TextView] with id "tvStreetTitle" 
       arrayListOf() //Your list goes here
)
autoTextView.threshold = 1 //will start working from first character
autoTextView.setAdapter(adapter)

Android AutoCompleteTextView Custom Layout and Adapter, In this post, you can learn how to create custom AutoCompleteTextView adapter, custom filter and custom item layout with item divider with� 3.0 Android AutocompleteTextView with Custom Adapter Example Codes. Now here’s the exciting part. We’ll dig deeper with each main files of the program, see the codes and some explanation. Enjoy! 3.1 activity_main.xml – see how our custom AutoCompleteTextView were put in the XML layout.

I believe that the easiest approach is to extend SimpleAdapter.

public class MyAdapter extends android.widget.SimpleAdapter {

    static ArrayList<Map<String, String>> toMapList(Collection<MyObject> objectsCollection) {
        ArrayList<Map<String, String>> objectsList = new ArrayList<Map<String, String>>(objectsCollection.size());
        for (MyObject obj : objectsCollection) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", obj.getName());
            map.put("details", obj.getDetails());
            objectsList.add(map);
        };
        return objectsList;
    }

    public MyAdapter(Context context, Collection<MyObject> objects) {

        super(context, toMapList(objects),
              R.layout.auto_complete_layout, new String[] {"name", "description"}, new int[] {R.id.name, R.id.description});
    }
}

The major drawback is that this will bring candidates based on any space-delimited word in either name or description. If you add another field to your auto_complete_layout, it will be involved in matching, too.

Therefore, I finished with rewriting the SimpleAdapter to better fit my needs, removing a significant amount of the base class overhead which was not relevant for my use case. But the few lines above give you a good start, and provide a solid reference to start customization from.

Android, android documentation: AutoComplete with CustomAdapter, ClickListener and ArrayList<People>(); list.add(new People("James", "Bond", 1)); list.add(new� AutoCompleteTextView Custom Adapter Class. GitHub Gist: instantly share code, notes, and snippets.

Always keep in mind that when you are customizing ArrayAdapter for your AutoCompleteTextView, you have to implement your own filtering method.

Autocomplete TextView in Android. AutocompleteTextView is an , The following code snippet shows how to create a auto complete text view Custom Adapter for Autocomplete TextView FruitAdapter.java: AutoCompleteTextView with Custom Adapter Part 2 music : Funky Junky by Audionautix is licensed under a Creative Commons Attribution license (https://creative

How to create a custom filtered adapter in Android — /var/, Android offers a nice component named AutoCompleteTextView that For this you must override the ArrayAdapter and add a custom Filter. Learn AutoCompleteTextView topic and how to use Adapter with example in Android Studio. In Android, AutoCompleteTextView is a view i.e similar to EditText, except that it displays a list of completion suggestions automatically while the user is typing.

AutoCompleteTextView, AutoCompleteTextView tutorial. Explained how to customize android AutoCompleteTextView Duration: 0:16 Posted: Sep 29, 2016 Android AutoCompleteTextView Example. Android AutoCompleteTextView completes the word based on the reserved words, so no need to write all the characters of the word.. Android AutoCompleteTextView is a editable text field, it displays a list of suggestions in a drop down menu from which user can select only one suggestion or value.

AutoCompleteTextView Custom Adapter Class � GitHub, package com.example.autocompletetextview; public class PeopleAdapter extends ArrayAdapter<People> {. Context context; filteredPeople.add(people);. Introduction. Android offers a nice component named AutoCompleteTextView that can be used to auto-fill a text box from a list of values. In its simplest form, you just create an array adapter passing it a list of objects (that have a proper toString() method).

Comments
  • I applied this method, however, no autocomplete appears.
  • can you override " extends ListAdapter & Filterable" to BaseAdapter ?, I say : adapter into adapter ?
  • @delive you can replace ListAdapter with whichever subclass you want.
  • Thank you! That was really helpful! I had a problem - dropdown list in my AutoCompleteTextView didn't show up and getView method of my custom adapter hadn't been called. All I needed to do was adding getFilter method as on this example