The form has EditText and ListView. There is a collection consisting of lines, and also there is a collection tmpArr, which through the adapter will be displayed in the ListView. But before displaying it in the ListView, you need to check whether the word entered into EditText is contained in a line from the collection and if it contains, add the word from the first collection to the tmpArr collection. And you need to check whether there is a starting from the first character. contains does not fit Do so
@Override protected void onResume() { super.onResume(); txtSearch.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { tmpArr.clear(); for (String s : rusArr) { int txtSearchLenght = charSequence.length(); String str = s.substring(0, txtSearchLenght); if (charSequence.equals(str)) { tmpArr.add(s); } } arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, tmpArr); list.setAdapter(arrayAdapter); } @Override public void afterTextChanged(Editable editable) { } }); } But when I start typing the second character in EditText, the program crashes. Here is a glass
java.lang.StringIndexOutOfBoundsException: length=1; regionStart=0; regionLength=2 at java.lang.String.substring(String.java:1931) at ru.mysite.project1.MainActivity$1.onTextChanged(MainActivity.java:136) at android.widget.TextView.sendOnTextChanged(TextView.java:8186) at android.widget.TextView.handleTextChanged(TextView.java:8248) at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10370) at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1208) at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:578) at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:509) at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:508) at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:844) at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:198) at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183) at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:345) at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)