Android列表视图搜索导致索引在片段中超出绑定异常

时间:2014-12-04 07:13:07

标签: android listview

您好我正在使用android。我正在尝试在我的应用程序中实现列表视图搜索。我有一个编辑文本并从该编辑文本中获取值并使用基本适配器更新我的列表视图。现在问题是它工作正常完美搜索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);
        }
    }

2 个答案:

答案 0 :(得分:2)

噢,这个问题与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方法获取。

答案 1 :(得分:1)

我认为问题与你的if条件有关。如果条件,请尝试检查内部的数组长度。试试这个

 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 ((textlength <= date[i].length() && a.
             equalsIgnoreCase((String) date[i].subSequence(0, textlength))) || (textlength <= descriptions[i].length() &&  a.
             equalsIgnoreCase((String) descriptions[i].subSequence(0, textlength)))|| (textlength <= time[i].length() &&  a.
             equalsIgnoreCase((String) time[i].subSequence(0, textlength)))|| (textlength <= place[i].length() &&  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));

     }
     });

希望这会帮助你的朋友:)