推进我的项目,我正在与一个我无法解决的问题作斗争..
想在 ListView 上运行搜索功能,所以我使用了 EditText。
我的问题:当我在 EditText 中获得两个字符并且仅显示包含我传递给它的“名称”的项目时,我的方法运行良好,但是当我获得第三个字符时,应用程序崩溃..
这是我的日志:
09-14 16:49:05.647: E/AndroidRuntime(20020): FATAL EXCEPTION: main
09-14 16:49:05.647: E/AndroidRuntime(20020): java.lang.StringIndexOutOfBoundsException: length=2; regionStart=0; regionLength=3
09-14 16:49:05.647: E/AndroidRuntime(20020): at java.lang.String.startEndAndLength(String.java:593)
09-14 16:49:05.647: E/AndroidRuntime(20020): at java.lang.String.substring(String.java:1474)
09-14 16:49:05.647: E/AndroidRuntime(20020): at java.lang.String.subSequence(String.java:1861)
09-14 16:49:05.647: E/AndroidRuntime(20020): at com.android.bordeaux.code.MyListActivity$3.onTextChanged(MyListActivity.java:126)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.widget.TextView.sendOnTextChanged(TextView.java:8315)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.widget.TextView.handleTextChanged(TextView.java:8362)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8725)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:420)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:29)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:667)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:188)
09-14 16:49:05.647: E/AndroidRuntime(20020): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:149)
09-14 16:49:05.647: E/AndroidRuntime(20020): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
09-14 16:49:05.647: E/AndroidRuntime(20020): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.os.Handler.dispatchMessage(Handler.java:99)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.os.Looper.loop(Looper.java:137)
09-14 16:49:05.647: E/AndroidRuntime(20020): at android.app.ActivityThread.main(ActivityThread.java:4517)
09-14 16:49:05.647: E/AndroidRuntime(20020): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 16:49:05.647: E/AndroidRuntime(20020): at java.lang.reflect.Method.invoke(Method.java:511)
09-14 16:49:05.647: E/AndroidRuntime(20020): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-14 16:49:05.647: E/AndroidRuntime(20020): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-14 16:49:05.647: E/AndroidRuntime(20020): at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count)
{
textlength = inputSearch.getText().length();
String query = inputSearch.getText().toString();
arr_sort.clear();
for(int i=0;i < currentArray.size();i++)
{
if(textlength <= currentArray.get(i).getName().length())
{
String[] wordArray = currentArray.get(i).getName().split(" ");
for (int j = 0; j < wordArray.length; j++)
{
if(query.equalsIgnoreCase((String) wordArray[j].subSequence(0, textlength)))
{
arr_sort.add(currentArray.get(i));
break;
}
}
}
}
list_adapter = new announcersListAdapter(MyListActivity.this, R.layout.tab_list_item, arr_sort);
setListAdapter(list_adapter);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
public void afterTextChanged(Editable s) {
}
});
感谢帮助我的家伙:)