Search code examples
androidmultiautocompletetextview

MultiAutoCompleteTextView strings getting "," after selecting string from drop down?


i integrated MultiAutoCompleteTextView in to my dictionary app so now after integrating it.

i got a problem like this as show in second screen shot...

so now i don't want that ","

as show in second screen shots after !

i am getting "," like this

!,

plz help me thank q

enter image description here

enter image description here

String[] str={"!","\"","#","$","$1","%","'","+",",","/"};

            MultiAutoCompleteTextView mt=(MultiAutoCompleteTextView)findViewById(R.id.searchEditText);

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

            ArrayAdapter<String> adp=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,str);

            mt.setThreshold(1);
            mt.setAdapter(adp);

Solution

  • The comma is coming from CommaTokenizer() called in this line mt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    To remove the comma you need to implement your own Tokenizer. This is an example:

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

    PS: I copied the code from this answer