AutoCompleteTextView过滤问题

发布于 2024-11-18 12:12:11 字数 3767 浏览 1 评论 0原文

我有一个 AutoCompleteTextView 和一个 自定义 AutoCompleteTextView 适配器。

我的列表有 20 个项目,所有项目都包含“vodafone”字符串。当我在 AutoCompleteTextView 中输入一个字符时,它会显示结果,而第二个字符时,它不会显示列表。 例如;

<br/>->V
<br/>-->AutoComplete DropDown menu appears
<br/>->Vo
<br/>-->AutoComplete DropDown disappears
<br/>->Vod
<br/>-->AutoComplete DropDown menu appears
<br/>->Voda
<br/>-->AutoComplete DropDown disappears
<br/>...etc

为什么会发生这种情况?有什么建议吗?

这是我的适配器:

package listadapters;

import java.util.ArrayList;
import java.util.Locale;

import objects.Item;
import utils.Logging;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
public class AutoCompleteTextAdapter2 extends BaseAdapter implements Filterable {

   Context _context;
   ArrayList<Item> _items = new ArrayList<Item>();

   public AutoCompleteTextAdapter2(Context context, ArrayList<Item> items) {

       _context = context;


       for (int i = 0; i < items.size(); i++) {
           _items.add(new Item(items.get(i)));
        }

       for (int i = 0; i < _items.size(); i++) {
            orig.add(new Item(_items.get(i)));
        }
       filter = new ItemFilter();

   }

   @Override
   public int getCount() {
       if (_items != null)
           return _items.size();
       else
           return 0;
   }

   @Override
   public Object getItem(int arg0) {
       return _items.get(arg0);
   }

   @Override
   public long getItemId(int arg0) {

       return 0;
   }

   @Override
   public View getView(int arg0, View arg1, ViewGroup arg2) {

       View gv;

       if (arg1 == null){

           gv = new View(_context);
       }else {
           gv = (View) arg1;
       }
       return gv;
   }



  @Override

  public Filter getFilter() {

      return filter;

  }

  private ItemFilter filter;

  ArrayList<Item> orig = new ArrayList<Item>();

  private class ItemFilter extends Filter {

      public ItemFilter() {

  }

  ArrayList<Item> results = new ArrayList<Item>();
  @Override
  protected FilterResults performFiltering(CharSequence constraint) {

      if(constraint != null)
          Logging.TraceMessage(constraint.toString(), "**", "1");
      FilterResults oReturn = new FilterResults();

      if (orig == null){
        for (int i = 0; i < _items.size(); i++) {
            orig.add(new Item(_items.get(i)));
        }
      }

      if (constraint != null){
          results.clear();
          if (orig != null && orig.size() > 0) {
              for (Item i : orig) {
                  if (i._itemName.toLowerCase(Locale.US).contains(constraint.toString().toLowerCase(Locale.US))){
                      results.add(new Item(i));
                  }
              }
          }
          Logging.TraceMessage(String.valueOf(results.size()), "**", "2");
          oReturn.values = results;
      }
      return oReturn;
  }


  @SuppressWarnings("unchecked")
  @Override
  protected void publishResults(CharSequence constraint, FilterResults results) {
      _items.clear();
      for(int i = 0; i < ((ArrayList<Item>)results.values).size(); i++){
          _items.add(new Item(((ArrayList<Item>)results.values).get(i)));
      }
      notifyDataSetChanged();
  }

}

 }

并设置适配器代码:

 AutoCompleteTextAdapter2 adapter = new AutoCompleteTextAdapter2(myAct, GlobalVariables.currCategoryItems);
yeniMarkaModel.setAdapter(adapter);

I have aAutoCompleteTextView and a custom AutoCompleteTextView adapter.

my list has 20 items and All items contains "vodafone" string. When i type one character in AutoCompleteTextView, it shows the results and on second, it does not shows the list. For example;

<br/>->V
<br/>-->AutoComplete DropDown menu appears
<br/>->Vo
<br/>-->AutoComplete DropDown disappears
<br/>->Vod
<br/>-->AutoComplete DropDown menu appears
<br/>->Voda
<br/>-->AutoComplete DropDown disappears
<br/>...etc

Why it's happening? Any suggestions?

here is my adapter:

package listadapters;

import java.util.ArrayList;
import java.util.Locale;

import objects.Item;
import utils.Logging;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
public class AutoCompleteTextAdapter2 extends BaseAdapter implements Filterable {

   Context _context;
   ArrayList<Item> _items = new ArrayList<Item>();

   public AutoCompleteTextAdapter2(Context context, ArrayList<Item> items) {

       _context = context;


       for (int i = 0; i < items.size(); i++) {
           _items.add(new Item(items.get(i)));
        }

       for (int i = 0; i < _items.size(); i++) {
            orig.add(new Item(_items.get(i)));
        }
       filter = new ItemFilter();

   }

   @Override
   public int getCount() {
       if (_items != null)
           return _items.size();
       else
           return 0;
   }

   @Override
   public Object getItem(int arg0) {
       return _items.get(arg0);
   }

   @Override
   public long getItemId(int arg0) {

       return 0;
   }

   @Override
   public View getView(int arg0, View arg1, ViewGroup arg2) {

       View gv;

       if (arg1 == null){

           gv = new View(_context);
       }else {
           gv = (View) arg1;
       }
       return gv;
   }



  @Override

  public Filter getFilter() {

      return filter;

  }

  private ItemFilter filter;

  ArrayList<Item> orig = new ArrayList<Item>();

  private class ItemFilter extends Filter {

      public ItemFilter() {

  }

  ArrayList<Item> results = new ArrayList<Item>();
  @Override
  protected FilterResults performFiltering(CharSequence constraint) {

      if(constraint != null)
          Logging.TraceMessage(constraint.toString(), "**", "1");
      FilterResults oReturn = new FilterResults();

      if (orig == null){
        for (int i = 0; i < _items.size(); i++) {
            orig.add(new Item(_items.get(i)));
        }
      }

      if (constraint != null){
          results.clear();
          if (orig != null && orig.size() > 0) {
              for (Item i : orig) {
                  if (i._itemName.toLowerCase(Locale.US).contains(constraint.toString().toLowerCase(Locale.US))){
                      results.add(new Item(i));
                  }
              }
          }
          Logging.TraceMessage(String.valueOf(results.size()), "**", "2");
          oReturn.values = results;
      }
      return oReturn;
  }


  @SuppressWarnings("unchecked")
  @Override
  protected void publishResults(CharSequence constraint, FilterResults results) {
      _items.clear();
      for(int i = 0; i < ((ArrayList<Item>)results.values).size(); i++){
          _items.add(new Item(((ArrayList<Item>)results.values).get(i)));
      }
      notifyDataSetChanged();
  }

}

 }

and set adapter code:

 AutoCompleteTextAdapter2 adapter = new AutoCompleteTextAdapter2(myAct, GlobalVariables.currCategoryItems);
yeniMarkaModel.setAdapter(adapter);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2024-11-25 12:12:11

在返回结果之前,您必须将过滤的项目数分配给 FilterResults.count。文本控件为下拉列表分配这么多行。

原因是 AutoCompleteTextView 从不直接使用 values 字段,而是要求您的 Adapter 获取具有特定索引的项目。例如,这使得始终可以将整个项目集合传递给 values,但仅从 Adapter.getItem() 返回其中的一些项目。

You MUST assign the number of items you filtered to the FilterResults.count before you return your results. The text control allocates this many lines for the drop-down.

The reason for this is that the AutoCompleteTextView never uses the values field directly but rather asks your Adapter to fetch an item with a specific index. This makes it possible, for instance, to always pass the whole collection of items to values but only return some of them from Adapter.getItem().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文