AutoCompleteTextView force to show all items

前端 未结 12 1737
隐瞒了意图╮
隐瞒了意图╮ 2020-12-25 08:10

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 somet

相关标签:
12条回答
  • 2020-12-25 08:27

    This works for me perfectly, this is an easy way to resolve the problem:

    final ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, usernameLists);
        etUsername.setThreshold(1);
        etUsername.setAdapter(adapter);
        etUsername.setOnTouchListener(new View.OnTouchListener() {
    
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
                if (usernameLists.size() > 0) {
                    // show all suggestions
                    if (!etUsername.getText().toString().equals(""))
                        adapter.getFilter().filter(null);
                    etUsername.showDropDown();
                }
                return false;
            }
        });
    
    0 讨论(0)
  • 2020-12-25 08:29

    To stop adapter from filtering you can set high enough treshold on your textview:

    autoCompleteTextView.setThreshold(100);
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-25 08:30
    class InstantAutoComplete(context: Context?, attrs: AttributeSet?) : AutoCompleteTextView(context, attrs) {
    
    override fun enoughToFilter(): Boolean {
        return true
    }
    
    override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect)
        if (focused && filters != null) {
            performFiltering(text, 0)
        }
    }
    
    fun performFilter() {
        performFiltering(text, 0)
        showDropDown()
    }
    

    }

    I need to show all data from autocompletetextview after click button dropdown(imageview). Using class extends AutoCompleteTextView and add this function, it works like magic.

    0 讨论(0)
  • 2020-12-25 08:31

    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:

        public class burtuAdapteris extends ArrayAdapter<String> implements Filterable {
    
           ArrayList<String> _items = new ArrayList<String>();
           ArrayList<String> orig = new ArrayList<String>();
    
           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) {
    
                      if(constraint != null)
                          Log.d("Constraints", constraint.toString());
                      FilterResults oReturn = new FilterResults();
    
                    /*  if (orig == null){
                        for (int i = 0; i < items.size(); i++) {
                            orig.add(items.get(i));
                        }
                      }*/
                      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++;
    
                                    }
                                }
                          }
                          Log.d("REsult size:" , String.valueOf(_items.size()));
                              if(!counters)
                              {
                                 _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 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>();

    0 讨论(0)
  • 2020-12-25 08:32

    Simple and easy answer is:

    after autoCompleteTextView.setText("") simply remove filter from adapter like this:

    autoCompleteTextView.adapter.filter.filter(null)

    thats it, filtering is gone and whole dropdown list is shown.

    0 讨论(0)
  • 2020-12-25 08:33

    method forcefully show drop down list.

    you need to call requestFocus(); to show keyboard otherwise keyboard does not pop up.

    autocomptv.setOnTouchListener(new OnTouchListener() {
    
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
                // TODO Auto-generated method stub
                autocomptv.showDropDown();
                autocomptv.requestFocus();
                return false;
            }
        });
    
    0 讨论(0)
提交回复
热议问题