tags:

views:

40

answers:

1

Hello experts,

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

A: 

Have you looked at AutoCompleteTextView?

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
           }

        }
    }
Greg
Hi Greg, I am going thru your code and trying to implement it in the way you described above. Meanwhile,I myself made some research and managed to generate something which works 99% perfect for me. The only issue I have with this code(pastebin link given) is that most of the times, the suggestions are shown only when the third letter is typed in the autocomplete widget. The code seems to be perfect from my perspective.Would be of tremendous help, if you can tell where I have gone wrong with the code given in - http://pastebin.com/d9TT1izC .By that time, I would try and finish your suggestion.
So your code actually is using this filterable implicitly. The fact that you want the as soon as you type functionality and the limit of 5 is where the custom work has to come in. So try setting the AutoCompleteTextView threshold to 0 and if you really really want to, extend the AutoCompleteTextView and override enoughToFilter() to always return true. The code I have above lets you do something to constrain the list to size 5. Otherwise it will be unbounded to the best matched
Greg
Did you figure it out?
Greg