在Android的动态自动完成构件 [英] Dynamic autocomplete widget in android

查看:107
本文介绍了在Android的动态自动完成构件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以亲切地指导我,如何实现在Android的动态自动完成小部件。我的要求是,当我键入一个字母,一个线程将运行,将返回最多5个建议的数组。我需要在自动完成列表视图中显示这5个建议。

Can anyone kindly guide me as to how to implement a dynamic auto complete widget in android. My requirement is, as I type a letter, a thread will work and will return an array with 5 suggestions maximum. I need to display these 5 suggestions in an auto complete list view.

专家请指导,如何实现相同的。

Experts please guide as to how to implement the same.

展望未来, 问候, 罗尼

Looking forward, Regards, Rony

推荐答案

你看AutoCompleteTextView?

Have you looked at AutoCompleteTextView?

这你想要做什么的显示。现在,所有你需要的是一个实现的可筛选产生的组五个适配器。可筛选说,一个实体将能够创建一个过滤器对象。过滤对象使用一个线程池,并派遣一个单独的工作线程过滤和绑定到UI线程上的看法。

It does the display of what you want. Now all you need is an adapter that implements Filterable to generate the set of five. Filterable says that an entity will be able to create a Filter object. Filter objects use a thread pool and dispatch the filtering on a separate worker thread and the binding to the view on the UI thread.

因此​​,让我们说我们有

So let's say we have

public class TextAdapter extends BaseAdapter implements Filterable {
   List<String> myList;
   Filter myFilter;
   TextAdapter(String[] strings) {
      myList = Arrays.asList(strings);
      myFilter = new MyFilter(myList);
   }

   ...
   // implement the BaseAdapter methods as needed to manage the list.
   //

   public void setContents(List<String> strs) {
     myList.clear();
     myList.addAll(strs);
     mFilter = new Filter(myList);
   }

   public Filter getFilter() {
     return myFilter;
   }

   private final class MyFilter implements Filter {
       final List<String> mOriginalList;
       public MyFilter(List<String> list) {
          mOriginalList = new ArrayList<String>(list);
       }

       public Filter.FilterResults performFiltering(CharSequence constraint) {
               // Search through your original list 
               Filter.FilterResults results = new Filter.FilterResults();
               List<String> strs = new ArrayList<String>();
               if (TextUtils.isEmpty(constraint)) {
                   strs.addAll(myOriginalList);
               }
               for (String str : myOriginalList) {
                  if (matches(str, constraint)) {
                   strs.add(str);
                  }
               }
               if (results.size > 5) {
                  // prune the list to your own tastes

               }
               results.count = strs.size();
               results.value = strs;
       }

       public void publishResults(CharSequence constraint, Filter.FilterResults results)
            setContents((List<String>)results.value);
            notifyDatasetChanged();
       }

       public boolean matches(String str, CharSequence value) {
          /// implement this part
           }

        }
    }

这篇关于在Android的动态自动完成构件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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