gpt4 book ai didi

Android listview 搜索导致 fragment 中的索引越界异常

转载 作者:太空狗 更新时间:2023-10-29 12:40:20 25 4
gpt4 key购买 nike

您好我正在使用 android。我正在尝试在我的应用程序中实现 ListView 搜索。我有一个编辑文本并从该编辑文本中获取值并使用基本适配器更新我的 ListView 。现在的问题是它有效完全最多搜索 4 个字符。当我输入第 5 个字符时,它强制关闭并显示索引超出范围异常。我搜索了很多并找到了答案“删除 getcount”方法。但它仍然没有解决我的问题。我该如何解决这个问题?提前致谢:)

 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);
}
}

最佳答案

天哪,问题出在你的 Subsequence 方法上,看来你正试图在它 startsWith 时进行匹配你可以像这样使用你的 if 条件

String a=search.getText().toString().toLowerCase();
if (
date[i].toLowerCase().startsWith(a) ||
descriptions[i].toLowerCase().startsWith(a) ||
time[i].toLowerCase().startsWith(a) ||
place[i].toLowerCase().startsWith(a)
)

您还可以使用 contains 方法来获取您的搜索字符串是否存在于目标字符串中。

关于Android listview 搜索导致 fragment 中的索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27288249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com