Autocompletetextview Not Completing Words Inside Parentheses

AutoCompleteTextView not completing words inside parentheses

The default implementation of the filter for ArrayAdapter is searching the beginning of words (separated by space), I mean it uses startsWith. You will need to implement an ArrayFilter which uses contains together with startsWith.

Your issue will be solved by the following steps:

  • Download the ArrayAdapter.java file from here
  • Add that file into the project (you can refactor by renaming file to CustomArrayAdapter.java, for example).
  • In the file, you will find a private class ArrayFilter. Then, add valueText.contains(prefixString) and words[k].contains(prefixString) as the following:

                if (valueText.startsWith(prefixString) || valueText.contains(prefixString)) {
    newValues.add(value);
    } else {
    final String[] words = valueText.split(" ");
    final int wordCount = words.length;
    // Start at index 0, in case valueText starts with space(s)
    for (int k = 0; k < wordCount; k++) {
    if (words[k].startsWith(prefixString) || words[k].contains(prefixString)) {
    newValues.add(value);
    break;
    }
    }
    }
  • Use that customized ArrayAdapter for your AutoCompleteTextView

And here is the result screenshot:

AutoCompleteTextView

Hope this helps!

Custom AutoCompleteTextView behavior

You would need to write a custom Filter class and implement the performFiltering method yourself. This method takes a CharSequence argument, which you can use to perform whatever String operations you need in order to generate a list of matches from your dataset (in your case, you could use String.contains instead of String.startsWith). The performFiltering function is not run on the UI thread.

You then return your list of matches as a FilterResults object, which contains an Object values (your list of matches, probably an ArrayList) and an int count which is the size of your list of matches.

Finally, implement the publishResults callback method, which returns once the worker thread has generated the list of matches, allowing you to call notifyDataSetChanged on your AutoCompleteTextView's adapter so that it can display the results.

AutoCompleteTextView: Filter results from a previously made list of suggestions

The main point here is that you need to implement a customized ArrayFilter which uses contains together with/instead of startsWith.

I have an answer at the following question, please take a look:

AutoCompleteTextView not completing words inside parentheses

Hope this helps!

how to show suggestions of autocomplete Text Box contains with??

You need to extend the ArrayAdapter that implements Filterable.
Check this SO question and the answer: Filtering AutoCompleteTextView to show partial match

So your solution would be (the combination of question and answer):

public class CodesArrayAdapter extends ArrayAdapter implements Filterable{

List<String> allCodes;
List<String> originalCodes;
StringFilter filter;

public CodesArrayAdapter(Context context, int resource, List<String> keys) {
super(context, resource, keys);
allCodes=keys;
originalCodes=keys;
}

public int getCount() {
return allCodes.size();
}

public Object getItem(int position) {
return allCodes.get(position);
}

public long getItemId(int position) {
return position;
}

private class StringFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {

String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<String> list = originalCodes;

int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;

for (int i = 0; i < count; i++) {
filterableString = list.get(i);
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(filterableString);
}
}

results.values = nlist;
results.count = nlist.size();
return results;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
allCodes = (ArrayList<String>) results.values;
notifyDataSetChanged();
}

}

@Override
public Filter getFilter()
{
return new StringFilter();
}
}


Related Topics



Leave a reply



Submit