AutoCompleteTextView力显示所有项目 [英] AutoCompleteTextView force to show all items

查看:210
本文介绍了AutoCompleteTextView力显示所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个在我的应用程序了一下,我需要强制显示所有项目中的建议列表,不管是什么用户输入的内容。我该怎么办呢?

我试着做一些与过滤,但对我作为一个初学者过滤只是太复杂了,我试图寻找初学者教程过滤,没有任何运气。也许,还有一个更简单的方法来强制显示所有的建议项目?

编辑: 基本上什么是我的主意,是这样的,当用户键入的东西什么不在列表中,它显示了所有他能有可用的选项。

我发现检查天气的最好办法ACTV是beign显示与否,但onTextChangeEvent我比我的列表中的用户键入的文本,然后如果没有元素已被发现显示完整列表。

 公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数)
         {
           最后的EditText EDITTEXT =(EditText上)findViewById(R.id.vardsUserInput);
            。字符串将strValue = editText.getText()的toString()与toUpperCase()。
            字符串温度;
            INT CC = 0; //我的计数变量
            的for(int i = 0; I< vardi.length;我++)
            {
                TEMP =瓦迪[I] .toUpperCase();
                如果(temp.startsWith(strValue.toUpperCase()))
                {
                    Log.d(测试,瓦迪[I]);
                    CC ++;
                }
            }
        如果(抄送== 0)
        {
        //显示所有可用的选项
    textView.showDropDown();
         }
}
 

解决方案

基本上,经过5-6小时的实验,以了解这该死的过滤器是如何工作的,我写我自己的适配器,它不正是我想要的:

 公共类burtuAdapteris扩展ArrayAdapter<字符串>实现过滤的{

       ArrayList的<字符串> _items =新的ArrayList<字符串>();
       ArrayList的<字符串>原稿=新的ArrayList<字符串>();

       公共burtuAdapteris(上下文的背景下,INT资源的ArrayList<字符串>项目){
           超(背景下,资源,项目);

           的for(int i = 0; I< items.size();我++){
                orig.add(items.get(ⅰ));
            }
       }

       @覆盖
       公众诠释getCount将(){
           如果(_items!= NULL)
               返回_items.size();
           其他
               返回0;
       }

       @覆盖
       公共字符串的getItem(INT为arg0){
           返回_items.get(为arg0);
       }


      @覆盖

      公共过滤用getFilter(){
          过滤器过滤器=新的过滤器(){
              @覆盖
              保护FilterResults performFiltering(CharSequence的约束){

                  如果(约束!= NULL)
                      Log.d(约束,constraint.toString());
                  FilterResults oReturn =新FilterResults();

                / *如果(原稿== NULL){
                    的for(int i = 0; I< items.size();我++){
                        orig.add(items.get(ⅰ));
                    }
                  } * /
                  字符串温度;
                  诠释计数器= 0;
                  如果(约束!= NULL){

                      _items.clear();
                      如果(原稿=空&安培;!&安培;原稿尺寸()大于0){
                          的for(int i = 0; I<原稿尺寸();我++)
                            {
                                临时= orig.get(我).toUpperCase();

                                如果(temp.startsWith(constraint.toString()。与toUpperCase()))
                                {

                                     _items.add(orig.get(ⅰ));
柜台++;

                                }
                            }
                      }
                      Log.d(结果的大小:,将String.valueOf(_items.size()));
                          如果(!专柜)
                          {
                             _items.clear();
                             _items =原稿;
                          }
                      oReturn.values​​ = _items;
                      oReturn.count = _items.size();
                  }
                  返回oReturn;
              }


              @燮pressWarnings(未登记)
              @覆盖
              保护无效publishResults(CharSequence的约束,FilterResults结果){
                  如果(结果= NULL和放大器;!&安培; results.count> 0){
                        notifyDataSetChanged();
                        }
                        其他 {
                            notifyDataSetInvalidated();
                        }

              }

            };

          返回过滤器;

      }


 }
 

和它使用简单,只需更换原有的适配器这样的:

 最后burtuAdapteris fAdapter =新burtuAdapteris(这一点,android.R.layout.simple_dropdown_item_1line,清单当然);
 

在我的情况清单当然是:的ArrayList<字符串>清单当然=新的ArrayList<字符串>();

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

解决方案

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>();

这篇关于AutoCompleteTextView力显示所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆