0

My problem is that when I suggest sentences in MultiAutoCompleteTextView, when I press spacebar, the suggestions disappear. Example:

Suggested words:

THE ROCK
THE BALL
THERMAL

If I write "THE", all sentences are displayed, but if I write "THE " (with blank space) the suggestions are dismissed. I want that if you write "THE ", the elements "THE ROCK" and "THE BALL" are displayed in suggested words.

Thanks.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
cnbandicoot
  • 372
  • 3
  • 13

2 Answers2

1

You should implement MultiAutoCompleteTextView.Tokenizer and create a spaceTokenizer as below. Then set multiAutoCompleteTextView.setTokenizer(new SpaceTokenizer());

public class SpaceTokenizer implements MultiAutoCompleteTextView.Tokenizer {

public int findTokenStart(CharSequence text, int cursor) {
    int i = cursor;

    while (i > 0 && text.charAt(i - 1) != ' ') {
        i--;
    }
    while (i < cursor && text.charAt(i) == ' ') {
        i++;
    }

    return i;
}

public int findTokenEnd(CharSequence text, int cursor) {
    int i = cursor;
    int len = text.length();

    while (i < len) {
        if (text.charAt(i) == ' ') {
            return i;
        } else {
            i++;
        }
    }

    return len;
}

public CharSequence terminateToken(CharSequence text) {
    int i = text.length();

    while (i > 0 && text.charAt(i - 1) == ' ') {
        i--;
    }

    if (i > 0 && text.charAt(i - 1) == ' ') {
        return text;
    } else {
        if (text instanceof Spanned) {
            SpannableString sp = new SpannableString(text + " ");
            TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
                    Object.class, sp, 0);
            return sp;
        } else {
            return text + " ";
        }
    }
}
} 
rabhis
  • 440
  • 2
  • 5
0

Have a look at this post:

https://groups.google.com/forum/#!topic/android-developers/OrsN2xRpDmA I just ran into a similar problem and wrote a simple multi word derivation. It defaults to a "," separator but you can set it whatever you like using the "setSeparator" method. or this stack-overflow answer might be helpful cause it experiences the same problem: AutoCompleteTextView backed by CursorLoader

Community
  • 1
  • 1
Shobhit
  • 1,096
  • 11
  • 30