AutoCompleteTextView not completing words inside parentheses

后端 未结 6 406
灰色年华
灰色年华 2020-12-03 00:11

I have implemented AutoCompleteTextView as follows:

MainActivity.java

...
    public static String[] myData=new String[         


        
6条回答
  •  感动是毒
    2020-12-03 00:47

    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:

    Hope this helps!

提交回复
热议问题