我在更新我的列表视图 ontextchanged 时遇到问题。当我输入一个字母时工作正常,但在第二个字母上应用程序崩溃。可能是什么问题?我已经测试过并且 dolzina 或长度是正确的,但是为什么在 edittext 中的第二个插入字符上应用程序崩溃了?适配器会不会有问题?这是代码!
public class Main extends ListActivity {
private ArrayList<Son> sonovi;
private EditText filterText = null;
private SonovnikAdapter adapter;
private ListView list;
Translator t = new Translator();
private Intent intent;
private ArrayList<Son> temp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initStuff();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
intent.putExtra("opis", sonovi.get(position).getOpis());
intent.putExtra("naslov", sonovi.get(position).getNaslov());
startActivity(intent);
}
});
filterText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
int dolzina = filterText.length();
temp = new ArrayList<Son>();
temp.clear();
for (int i = 0; i < sonovi.size(); i++) {
if (filterText.getText().toString().toLowerCase()
.equals((String) sonovi.get(i).getLatinicno()
.toLowerCase().subSequence(0, dolzina))) { //line 63
temp.add(sonovi.get(i));
}
}
SonovnikAdapter testc = new SonovnikAdapter(Main.this,
R.layout.item, temp);
list.setAdapter(testc);
testc.notifyDataSetChanged();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
intent.putExtra("opis", temp.get(position).getOpis());
intent.putExtra("naslov", temp.get(position)
.getNaslov());
startActivity(intent);
}
});
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private ArrayList<Son> readTxt() {
ArrayList<Son> s = new ArrayList<Son>();
InputStream is = this.getResources().openRawResource(R.raw.sonovnik);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = null;
String naslov, opis, latinica;
String[] tmp;
try {
while ((str = br.readLine()) != null) {
tmp = str.split("-");
naslov = tmp[0];
opis = tmp[1];
if (tmp.length >= 3) {
latinica = tmp[2];
s.add(new Son(naslov, opis, latinica));
}
}
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
private void initStuff() {
list = getListView();
list.setTextFilterEnabled(true);
sonovi = new ArrayList<Son>();
sonovi = readTxt();
intent = new Intent(this, Details.class);
adapter = new SonovnikAdapter(this, R.layout.item, sonovi);
setListAdapter(adapter);
filterText = (EditText) findViewById(R.id.search_box);
}
private class SonovnikAdapter extends ArrayAdapter<Son> {
private ArrayList<Son> items;
public SonovnikAdapter(Context context, int textViewResourceId,
ArrayList<Son> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
Son o = items.get(position);
if (o != null) {
TextView naslov = (TextView) v.findViewById(R.id.textView1);
if (naslov != null) {
naslov.setText(o.getNaslov().toString());
}
}
return v;
}
}
}
以及 logcat 的输出:
05-07 15:16:58.770: E/AndroidRuntime(31689): FATAL EXCEPTION: main
05-07 15:16:58.770: E/AndroidRuntime(31689): java.lang.StringIndexOutOfBoundsException
05-07 15:16:58.770: E/AndroidRuntime(31689): at java.lang.String.substring(String.java:1651)
05-07 15:16:58.770: E/AndroidRuntime(31689): at java.lang.String.subSequence(String.java:2040)
05-07 15:16:58.770: E/AndroidRuntime(31689): at com.sonovnik.petkovski.Main$2.onTextChanged(Main.java:63)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.widget.TextView.sendOnTextChanged(TextView.java:6321)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.widget.TextView.handleTextChanged(TextView.java:6362)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6511)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:502)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:654)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:415)
05-07 15:16:58.770: E/AndroidRuntime(31689): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:318)
05-07 15:16:58.770: E/AndroidRuntime(31689): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:75)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.os.Looper.loop(Looper.java:130)
05-07 15:16:58.770: E/AndroidRuntime(31689): at android.app.ActivityThread.main(ActivityThread.java:3835)
05-07 15:16:58.770: E/AndroidRuntime(31689): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 15:16:58.770: E/AndroidRuntime(31689): at java.lang.reflect.Method.invoke(Method.java:507)
05-07 15:16:58.770: E/AndroidRuntime(31689): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
05-07 15:16:58.770: E/AndroidRuntime(31689): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
05-07 15:16:58.770: E/AndroidRuntime(31689): at dalvik.system.NativeStart.main(Native Method)