I have a list that will filter in input in an EditText field and populate the ListView according to what they type.
For example, if the user types “al” it will populate names that have a first or last name beginning with “al.”
My problem is when the user backspaces the EditText field all the way back where no letters are typed, my program crashes. I’m not sure why my initial if statement isn’t getting used.
This is NOT an issue of the ListView being empty causing a crash, because if I type a name that isn’t in the list, the ListView will stay empty and not crash.
private void AlterAdapter() {
partialNames.clear();
if (searchName.getText().toString() == "")
partialNames.add("There are no names that meet your criteria");
else {
String seekString = searchName.getText().toString();
String firstLetter = seekString.substring(0,1).toString().toUpperCase();
Log.d("JAY", "The firstLetter is: " + firstLetter);
if (!seekString.isEmpty() && seekString.length() == 1) {
//firstLetter = seekString.toUpperCase();
for (int i = 0; i < searchNames.size(); i++) {
if (searchNames.get(i).toString().contains(firstLetter)) {
partialNames.add(searchNames.get(i).toString());
}
}
}
else if (!seekString.isEmpty() && seekString.length() > 1) {
//searchCriteria = searchCriteria.concat(seekString);
String searchWord = firstLetter.concat(seekString.substring(1,seekString.length()));
Log.d("JAY", "The seekString is " + seekString);
Log.d("JAY", "The searchWord is " + searchWord);
for (int i = 0; i < searchNames.size(); i++) {
if (searchNames.get(i).toString().contains(searchWord)) {
partialNames.add(searchNames.get(i).toString());
}
}
}
}
adapter.notifyDataSetChanged();
}
Here is the logcat:
D/AndroidRuntime( 1518): Shutting down VM
W/dalvikvm( 1518): threadid=1: thread exiting with uncaught exception
(group=0x40015560)E/AndroidRuntime( 1518): FATAL EXCEPTION: main
E/AndroidRuntime( 1518): java.lang.StringIndexOutOfBoundsException
E/AndroidRuntime( 1518): at
java.lang.String.substring(String.java:1651)E/AndroidRuntime( 1518): at
com.jaylefler.contacts.SearchContacts.AlterAdapter(SearchContacts.java:92)E/AndroidRuntime( 1518): at
com.jaylefler.contacts.SearchContacts.access$0(SearchContacts.java:88)E/AndroidRuntime( 1518): at
com.jaylefler.contacts.SearchContacts$1.onTextChanged(SearchContacts.java:73)E/AndroidRuntime( 1518): at
android.widget.TextView.sendOnTextChanged(TextView.java:6295)E/AndroidRuntime( 1518): at
android.widget.TextView.handleTextChanged(TextView.java:6336)E/AndroidRuntime( 1518): at
android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6485)E/AndroidRuntime( 1518): at
android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)E/AndroidRuntime( 1518): at
android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:400)E/AndroidRuntime( 1518): at
android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)E/AndroidRuntime( 1518): at
android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)E/AndroidRuntime( 1518): at
android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:218)E/AndroidRuntime( 1518): at
android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:28)E/AndroidRuntime( 1518): at
android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:60)E/AndroidRuntime( 1518): at
android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:123)E/AndroidRuntime( 1518): at
android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:327)E/AndroidRuntime( 1518): at
android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:132)E/AndroidRuntime( 1518): at
android.widget.TextView.doKeyDown(TextView.java:4433)E/AndroidRuntime( 1518): at
android.widget.TextView.onKeyDown(TextView.java:4267)E/AndroidRuntime( 1518): at
android.view.KeyEvent.dispatch(KeyEvent.java:1256)E/AndroidRuntime( 1518): at
android.view.View.dispatchKeyEvent(View.java:3855)E/AndroidRuntime( 1518): at
android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)E/AndroidRuntime( 1518): at
android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)E/AndroidRuntime( 1518): at
android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)E/AndroidRuntime( 1518): at
android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)E/AndroidRuntime( 1518): at
android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)E/AndroidRuntime( 1518): at
android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)E/AndroidRuntime( 1518): at
com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687)E/AndroidRuntime( 1518): at
com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120)E/AndroidRuntime( 1518): at
android.app.Activity.dispatchKeyEvent(Activity.java:2073)E/AndroidRuntime( 1518): at
com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)E/AndroidRuntime( 1518): at
android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)E/AndroidRuntime( 1518): at
android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)E/AndroidRuntime( 1518): at
android.view.ViewRoot.handleMessage(ViewRoot.java:1867)E/AndroidRuntime( 1518): at
android.os.Handler.dispatchMessage(Handler.java:99)E/AndroidRuntime( 1518): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1518): at
android.app.ActivityThread.main(ActivityThread.java:3683)E/AndroidRuntime( 1518): at
java.lang.reflect.Method.invokeNative(Native Method)E/AndroidRuntime( 1518): at
java.lang.reflect.Method.invoke(Method.java:507)E/AndroidRuntime( 1518): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)E/AndroidRuntime( 1518): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)E/AndroidRuntime( 1518): at dalvik.system.NativeStart.main(Native
Method)W/ActivityManager( 74): Force finishing activity
com.jaylefler.contacts/.SearchContactsW/ActivityManager( 74): Activity pause timeout for
HistoryRecord{4086e540 com.jaylefler.contacts/.SearchContacts}I/Process ( 1518): Sending signal. PID: 1518 SIG: 9
I/ActivityManager( 74): Process com.jaylefler.contacts (pid 1518)
has died.E/InputDispatcher( 74): channel ‘409a83e8
com.jaylefler.contacts/com.jaylefler.contacts.ContactProjectActivity
(server)’ ~ Consumer closed input channel or an error occurred.
events=0x8E/InputDispatcher( 74): channel ‘409a83e8
com.jaylefler.contacts/com.jaylefler.contacts.ContactProjectActivity
(server)’ ~ Channel is unrecoverably broken and will be disposed!I/WindowManager( 74): WIN DEATH: Window{4086ff40
com.jaylefler.contacts/com.jaylefler.contacts.SearchContacts
paused=false}I/WindowManager( 74): WIN DEATH: Window{409a83e8
com.jaylefler.contacts/com.jaylefler.contacts.ContactProjectActivity
paused=false}E/InputDispatcher( 74): Received spurious receive callback for
unknown input channel. fd=150, events=0x8W/InputManagerService( 74): Got RemoteException sending
setActive(false) notification to pid 1518 uid 10030
I think the prob;em is that you use ‘==’ for comparing searchName text with empty string. Such a comparison will always produce false. Use equals method instead or compare text length with 0.