推进我的项目,我正在解决一个我无法解决的问题。
想要在ListView上运行搜索功能,所以我使用了EditText。
我的问题:当我在EditText中获得两个字符并且只显示我传递给它的“Name”的项目时,我的方法很有效,但是当我得到第三个字符时,应用程序崩溃..
这是我的日志:
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) {
}
});
感谢帮助我们:)
答案 0 :(得分:1)
如果你改变这一行,它会有效吗?
if(query.equalsIgnoreCase((String) wordArray[j].subSequence(0, textlength)))
与
if(query.equalsIgnoreCase(wordArray[j].substring(0,
Math.min(wordArray[j].length(), textlength))))