How to replace the comma with a space when I use the "MultiAutoCompleteTextView"
Asked Answered
A

4

20

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!

An answered 14/8, 2010 at 10:55 Comment(0)
B
48
public class SpaceTokenizer implements 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 + " ";
    }
}
}
}
Barry answered 4/1, 2011 at 17:44 Comment(5)
I am doing something similar HERE!! https://mcmap.net/q/138921/-autocompletetextview-backed-by-cursorloaderGoldina
In findTokenStart, you iterate backward, then forward .... looks like once? Why do this?Neckwear
you should probably use Character.isWhitespace instead of comparing to ' ' this way it works with newlines and tabs etcDichotomous
Where to call class ?Yuille
Call it like this mactv.setTokenizer(new SpaceTokenizer());Chamois
T
2

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.

Terrill answered 14/8, 2010 at 14:24 Comment(3)
oh,thank you. But when I include the "MultiAutoCompleteTextView.java",I encounter an error "com.android.internal.R cannot be resolved",and I cann't find this file...An
Tokenizer link is broken.Representation
@Daniel, Can you update the link for source code of tokenizer? link is broken.Orth
B
1

Check my question/answer

How to replace MultiAutoCompleteTextView drop down list

you will find a SpaceTokenizer class

Barry answered 11/12, 2010 at 4:19 Comment(4)
Is it possible to implement this without using tokenizers ? I mean for example if i do not want any comma or space as tokenizers then will the spannable functionality work as it is ?Sizing
You can change separator value with any character that you want. Don't know the reason why you don't want to user tokenizer, but you can remove tokenizer instance if you want.Barry
I changed to space tokenizer, but if my striings (in multi auto complete text view) already contain spaces .e.g. person name, then do I need to take some other tokenizer OR space will work fine in this scneario too ?Sizing
In that case SpaceTokenizer wont work. You must implement your own tokenizer with a more clever behavior.Barry
H
0

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 "
                    }
                }
            }

        }
Humoresque answered 16/2, 2022 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.