AutoCompleteTextView强制显示所有项目

XCo*_*der 10 java android autocompletetextview

在我的应用程序中有一个时刻,我需要强制显示建议列表中的所有项目,无论用户输入了什么.我怎样才能做到这一点?

我试图用过滤做一些事情,但对于我来说,初学者过滤太复杂了,我尝试搜索初学者教程进行过滤而没有任何运气.也许,有一种更简单的方法可以强制显示所有建议项目?

编辑:基本上我的想法是,当用户键入不在列表中的内容时,它会显示他可以拥有的所有可用选项.

我已经找到了检查天气的最佳方式,ACTV是否显示,但onTextChangeEvent我将用户输入的文本与我的列表进行比较,然后如果没有找到任何元素显示完整列表.

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();                    
         }                  
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*Sam 13

当你想要显示所有结果时,你没有定义"时刻",所以我希望这适合.但尝试这样的事情:

AutoCompleteTextView autoComplete;
String savedText;

public void showAll() {
    savedText = autoComplete.getText().toString();
    autoComplete.setText("");
    autoComplete.showDropDown();
}

public void restore() {
    autoComplete.setText(savedText);
}
Run Code Online (Sandbox Code Playgroud)


小智 9

要阻止适配器过滤,您可以在 textview 上设置足够高的阈值:

autoCompleteTextView.setThreshold(100);
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。


XCo*_*der 7

基本上,经过5-6小时的实验来了解该死的过滤器是如何工作的,我编写了自己的适配器,它完全符合我的要求:

    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;

      }


 }
Run Code Online (Sandbox Code Playgroud)

它使用起来很简单,只需用以下方法替换原装适配器:

final burtuAdapteris fAdapter = new burtuAdapteris(this, android.R.layout.simple_dropdown_item_1line, liste);
Run Code Online (Sandbox Code Playgroud)

在我的情况下,监听是: ArrayList<String> liste = new ArrayList<String>();

  • 我正在复制和粘贴,但留下名字BurtuAdapter向你的工作致敬.:) (2认同)

Ho *_*ong 6

这非常适合我,这是解决问题的简便方法:

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;
        }
    });
Run Code Online (Sandbox Code Playgroud)