0

I'm trying to get my AutocompleteTextview to return all results containing the sub-string provided, however it only returns the results that START with the sub-string provided.

The current result set is

0:  "CMPN0004:Gigrig"
1:  "CMPN0002:NYDA"
2:  "CMPN0003:TEST"

and it correctly performs the autocomplete if I type "CMP", however it does not provide any results if I type "TEST" or "Gigrig"

final AutoCompleteTextView campaignTxt = (AutoCompleteTextView)findViewById(R.id.CampaignTxt);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(TripAddActivity.this,android.R.layout.simple_dropdown_item_1line, campaigns);
Pat
  • 13
  • 2

1 Answers1

0

You need to override getFilter, as described here:

You are having problem, mainly because you are using custom object. If you pass a String or int value to array adapter its know how to filter it. But if you pass custom object default filter implementation have to no idea how to deal with that.

Although it is not clear what you are trying to do in your filter i recommend you following steps.

  1. Proper implementation of ListTO, although it has nothing to do with your goal right now
  2. Implement custom filter
  3. return your filter

Implement custom filter

First thing you have to do is, implements Filterable from your array adapter.

Second, provide implementation of your Filter

Filter myFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
           FilterResults filterResults = new FilterResults();   
           ArrayList<ListTO> tempList=new ArrayList<ListTO>();
           //constraint is the result from text you want to filter against. 
           //objects is your data set you will filter from
           if(constraint != null && objects!=null) {
               int length=objects.size();
               int i=0;
                while(i<length){
                  ListTO item=objects.get(i);
                      //do whatever you wanna do here
                  //adding result set output array     

                      tempList.add(item);

                      i++;
                }
                //following two lines is very important
                //as publish result can only take FilterResults objects
                filterResults.values = tempList;
                filterResults.count = tempList.size();
          }
          return filterResults;
      }

      @SuppressWarnings("unchecked")
      @Override
      protected void publishResults(CharSequence contraint, FilterResults results) {
        objects = (ArrayList<ListTO>) results.values;
          if (results.count > 0) {
             notifyDataSetChanged();
          } else {
              notifyDataSetInvalidated();
          }  
      }
   };

Last step,

@Override
   public Filter getFilter() {
      return myFilter;
  }
Community
  • 1
  • 1
Marius Kaunietis
  • 674
  • 4
  • 17
  • Thank you for the swift response. I'm implementing that code now. Would you mind explaining why the adapter works when I use the first characters of the records but does not work when I use the middle characters? – Pat Feb 22 '16 at 11:51
  • It's simple - default Filter implementation works that way. You can see for yourself. Lines 483 and 491: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/ArrayAdapter.java#ArrayAdapter.ArrayFilter – Marius Kaunietis Feb 22 '16 at 12:05