javaandroidautocompletetextview

AutoCompleteTextView force to show all items


There is a moment in my app, that I need to force to show all items in the suggestion list, no matter what the user has typed. How can I do that?

I tried to do something with filtering, but for me as a beginner filtering is just way too complicated, I tried searching beginners tutorial for filtering without any luck. Maybe, there is a simpler way to force to show all the suggestion items?

EDIT: Basically what was my idea, is that, when user types something whats not in the list, it shows all the available options he can have.

I've found the best way of checking weather the ACTV is beign shown or not, but onTextChangeEvent I compare the user typed text with my list, and then if no elements have been found show full list.

public void onTextChanged(CharSequence s, int start, int before, int count)
         {                
           final EditText editText = (EditText) findViewById(R.id.vardsUserInput);
            String strValue = editText.getText().toString().toUpperCase();
            String temp;
            int Cc=0; //my count variable
            for(int i=0; i<vardi.length; i++)
            {
                temp = vardi[i].toUpperCase();
                if(temp.startsWith(strValue.toUpperCase()))
                {
                    Log.d("testing",vardi[i]);
                    Cc++;                                                   
                }
            }               
        if(Cc == 0)
        {
        //Show all the available options
    textView.showDropDown();                    
         }                  
}

Solution

  • Basically, after 5-6 hours of experimentation to understand how the damn filter works, I wrote my own adapter which does exactly what I want:

        import android.content.Context;
        import android.widget.ArrayAdapter;
        import android.widget.Filter;
        import android.widget.Filterable;
        
        import java.util.ArrayList;
        
        public class BurtuAdapteris extends ArrayAdapter<String> implements Filterable {
            ArrayList<String> _items = new ArrayList<>();
            ArrayList<String> orig = new ArrayList<>();
        
            public BurtuAdapteris(Context context, int resource, ArrayList<String> items) {
                super(context, resource, items);
        
                for (int i = 0; i < items.size(); i++) {
                    orig.add(items.get(i));
                }
            }
        
            @Override
            public int getCount() {
                if (_items != null)
                    return _items.size();
                else
                    return 0;
            }
        
            @Override
            public String getItem(int arg0) {
                return _items.get(arg0);
            }
        
            @Override
            public Filter getFilter() {
                Filter filter = new Filter() {
                    @Override
                    protected FilterResults performFiltering(CharSequence constraint) {
        
                        FilterResults oReturn = new FilterResults();
                        String temp;
                        int counters = 0;
                        if (constraint != null) {
        
                            _items.clear();
                            if (orig != null && orig.size() > 0) {
                                for (int i = 0; i < orig.size(); i++) {
                                    temp = orig.get(i).toUpperCase();
        
                                    if (temp.startsWith(constraint.toString().toUpperCase())) {
        
                                        _items.add(orig.get(i));
                                        counters++;
        
                                    }
                                }
                            }
                            if (counters == 0) {
                                _items.clear();
                                _items = orig;
                            }
                            oReturn.values = _items;
                            oReturn.count = _items.size();
                        }
                        return oReturn;
                    }
        
                    @SuppressWarnings("unchecked")
                    @Override
                    protected void publishResults(CharSequence constraint, FilterResults results) {
                        if (results != null && results.count > 0) {
                            notifyDataSetChanged();
                        } else {
                            notifyDataSetInvalidated();
                        }
        
                    }
        
                };
        
                return filter;
            }
        
        }
    

    And it's simple to use, just replace the original adapter with this:

        final BurtuAdapteris fAdapter = new burtuAdapteris(this, android.R.layout.simple_dropdown_item_1line, liste);
    

    In my case liste is: ArrayList<String> liste = new ArrayList<String>();