您好我正在使用android。我試圖在我的應用程序中實現列表視圖搜索。我有一個編輯文本並從該編輯文本中獲取值並更新我的列表視圖使用基礎適配器。現在的問題是,它可以完美地搜索4個字符。當我輸入第5個字符時,強制關閉並顯示索引超出限制的異常。我搜索了很多,然後回答「刪除getcount」方法。但它仍然不能解決我的問題。我該如何解決這個問題?感謝提前:)Android的listview搜索原因索引出界異常在片段
search.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{
}
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
textlength = search.getText().length();
date1.clear();
desc1.clear();
time1.clear();
place1.clear();
for (int i = 0; i < date.length; i++)
{
String a=search.getText().toString();
if (a.equalsIgnoreCase((String) date[i].subSequence(0, textlength)) ||
a.equalsIgnoreCase((String) descriptions[i].subSequence(0, textlength))||
a.equalsIgnoreCase((String) time[i].subSequence(0, textlength)) ||
a.equalsIgnoreCase((String) place[i].subSequence(0, textlength)))
{
date1.add(date[i]);
desc1.add(descriptions[i]);
time1.add(time[i]);
place1.add(place[i]);
}
}
listView.setAdapter(new dataListAdapter(date1,desc1,place1,time1));
}
});
這裏是我的日誌貓
12-04 12:56:52.786: E/AndroidRuntime(24240): java.lang.StringIndexOutOfBoundsException: length=4; regionStart=0; regionLength=5
12-04 12:56:52.786: E/AndroidRuntime(24240): at java.lang.String.startEndAndLength(String.java:588)
12-04 12:56:52.786: E/AndroidRuntime(24240): at java.lang.String.substring(String.java:1475)
12-04 12:56:52.786: E/AndroidRuntime(24240): at java.lang.String.subSequence(String.java:1862)
12-04 12:56:52.786: E/AndroidRuntime(24240): at info.androidhive.tabsswipe.UpcomingEvents$1.onTextChanged(UpcomingEvents.java:89)
12-04 12:56:52.786: E/AndroidRuntime(24240): at android.widget.TextView.sendOnTextChanged(TextView.java:7419)
12-04 12:56:52.786: E/AndroidRuntime(24240): at android.widget.TextView.handleTextChanged(TextView.java:7478)
12-04 12:56:52.786: E/AndroidRuntime(24240): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9198)
12-04 12:56:52.786: E/AndroidRuntime(24240): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
12-04 12:56:52.786: E/AndroidRuntime(24240): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
12-04 12:56:52.786: E/AndroidRuntime(24240): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
12-04 12:56:52.786: E/AndroidRuntime(24240): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
12-04 12:56:52.786: E/AndroidRuntime(24240): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:683)
12-04 12:56:52.786: E/AndroidRuntime(24240): at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:438)
這裏是我的適配器類
class dataListAdapter extends BaseAdapter {
String[] Date, Desc,Loc, Time;
dataListAdapter() {
Date = null;
Desc = null;
Loc = null;
Time = null;
}
public dataListAdapter(String[] text, String[] text1,String[] text3,String[] text4) {
Date = text;
Desc = text1;
Loc = text3;
Time = text4;
}
public dataListAdapter(ArrayList<String> text, ArrayList<String> text1,ArrayList<String> text2, ArrayList<String> text3) {
Date = new String[text.size()];
Desc = new String[text1.size()];
Loc = new String[text2.size()];
Time = new String[text3.size()];
for(int i=0;i<text.size();i++)
{
Date[i] = text.get(i);
Desc[i] = text1.get(i);
Loc[i] = text2.get(i);
Time[i] = text3.get(i);
}
}
public int getCount() {
return Date.length;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View row;
row = inflater.inflate(R.layout.event_item, parent, false);
TextView date, desc,loc,time;
date = (TextView) row.findViewById(R.id.date);
desc = (TextView) row.findViewById(R.id.desc);
loc = (TextView) row.findViewById(R.id.place);
time = (TextView) row.findViewById(R.id.time);
Typeface font = Typeface.createFromAsset(
getActivity().getAssets(),
"Ace.ttf");
date .setTypeface(font);
date.setText(Date[position]);
desc.setText(Desc[position]);
loc.setText(Loc[position]);
time.setText(Time[position]);
return (row);
}
}
請發表您的'logcat' – 2014-12-04 07:14:28
的logcat和列表視圖代碼還 – 2014-12-04 07:15:07
檢查我的編輯logcat的和適配器類添加 – 2014-12-04 07:32:37