0

I want to check if a String (coming from an EditText) is part of a String Array and I need the index. I have got a TextWatcher added as TextChangedListener like the following:

inside Activity-Class:

private String[] randomSpanishWords = { "pueblo", "madre" };

private TextWatcher ETListener = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        int index = Arrays.asList(randomSpanishWords).indexOf(charSequence);
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

in onCreate():

editText1.addTextChangedListener(ETListener);

No matter what Im typing in the EditText, index is always -1. What am I missing?

2
  • Have you debugged the value of charSequence? Unless it is equal to pueblo or madre, the result will always be -1. Commented Nov 9, 2014 at 23:23
  • Yes I have... thats why I dont get it... charSequence is pueblo if I type in pueblo Commented Nov 9, 2014 at 23:27

1 Answer 1

1

Try to convert your charSequence to String with

charSequence.toString()

Or use CharSequence for your array

private CharSequence[] randomSpanishWords = { "pueblo", "madre" };
Sign up to request clarification or add additional context in comments.

1 Comment

Great ! As I suspected, List.indexOf must check the class before comparing values with equals(). But I suspect this is android-specific

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.