'How to replace the comma with a space when I use the "MultiAutoCompleteTextView"

I'm doing a simple program using MultiAutoCompleteTextView to prompt the common words when I input several letters.

code:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_dropdown_item_1line, 
            ary);
    MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.editText);
    textView.setAdapter(adapter);

    textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    private String[] ary = new String[] {
       "abc",
       "abcd",
       "abcde",
       "abcdef",
       "abcdefg",
       "hij",
       "hijk",
       "hijkl",
       "hijklm",
       "hijklmn",
    };

Now,when I input 'a' and choose "abcd" but the result become to "abcd,". How to replace the comma with a space?

Thank you!



Solution 1:[1]

The way to do it would be to implement your own Tokenizer. The reason the comma comes up is because you're using CommaTokenizer, which is designed to do exactly that. You can also look at the source code for CommaTokenizer if you need a reference for how to implement your own SpaceTokenizer.

Solution 2:[2]

Check my question/answer

How to replace MultiAutoCompleteTextView drop down list

you will find a SpaceTokenizer class

Solution 3:[3]

I have used Kotlin for a white space tokenizer. This also caters tabs, trailing spaces and line feeds.

object : Tokenizer {
            override fun findTokenStart(text: CharSequence, cursor: Int): Int {
                val spaceIndex = text.indexOfFirst { Character.isWhitespace(it) }
                return if (spaceIndex > cursor)
                    spaceIndex
                else cursor
            }

            override fun findTokenEnd(text: CharSequence, cursor: Int): Int {
                val lastSpaceIndex = text.indexOfLast { Character.isWhitespace(it) }
                return if (lastSpaceIndex > cursor)
                    lastSpaceIndex
                else cursor
            }

            override fun terminateToken(text: CharSequence): CharSequence {
                val i = text.length

                return if (i > 0 && text[i - 1] == ' ') {
                    text
                } else {
                    if (text is Spanned) {
                        val sp = SpannableString("$text ")
                        TextUtils.copySpansFrom(
                            text as Spanned?, 0, text.length,
                            Any::class.java, sp, 0
                        )
                        sp
                    } else {
                        "$text "
                    }
                }
            }

        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dan Lew
Solution 2 Community
Solution 3 Ahmed Shahid