Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android listview搜索导致索引在片段中越界异常_Android_Listview - Fatal编程技术网

Android listview搜索导致索引在片段中越界异常

Android listview搜索导致索引在片段中越界异常,android,listview,Android,Listview,您好,我正在使用android。我正在尝试在我的应用程序中实现列表视图搜索。我有一个编辑文本,从编辑文本中获取值,并使用基本适配器更新我的列表视图。现在的问题是,它可以完美地搜索4个字符。当我输入第5个字符时,它强制关闭并显示索引越界异常。我搜索了很多,找到了一个答案“remove getcount”方法。但这仍然不能解决我的问题。我如何解决这个问题?提前感谢:) 这是我的适配器类 class dataListAdapter extends BaseAdapter { Strin

您好,我正在使用android。我正在尝试在我的应用程序中实现列表视图搜索。我有一个编辑文本,从编辑文本中获取值,并使用基本适配器更新我的列表视图。现在的问题是,它可以完美地搜索4个字符。当我输入第5个字符时,它强制关闭并显示索引越界异常。我搜索了很多,找到了一个答案“remove getcount”方法。但这仍然不能解决我的问题。我如何解决这个问题?提前感谢:)

这是我的适配器类

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);
        }
    }
类dataListAdapter扩展了BaseAdapter{ 字符串[]日期、描述、位置、时间; dataListAdapter(){ 日期=空; Desc=null; Loc=null; 时间=零; } 公共dataListAdapter(字符串[]文本、字符串[]文本1、字符串[]文本3、字符串[]文本4){ 日期=文本; Desc=text1; Loc=text3; 时间=文本4; } 公共dataListAdapter(ArrayList文本、ArrayList文本1、ArrayList文本2、ArrayList文本3){ 日期=新字符串[text.size()]; Desc=新字符串[text1.size()]; Loc=新字符串[text2.size()]; 时间=新字符串[text3.size()];
对于(int i=0;i噢,伙计,这是你的问题
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
方法获取搜索字符串是否存在于目标字符串中。

我认为问题在于您的if条件。请尝试检查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));

     }
     });
search.addTextChangedListener(新的TextWatcher()
{
公共无效后文本已更改(可编辑)
{
}
更改前的公共无效(字符序列、整数开始、,
整数计数,整数后)
{
}
public void onTextChanged(字符序列,int start,
前整数,整数计数)
{
textlength=search.getText().length();
date1.clear();
说明1.clear();
time1.clear();
位置1.清除();
对于(int i=0;i如果((textlength),请发布您的
logcat
logcat和列表视图代码,并选中我的编辑logcat和添加的适配器类
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)
    )
 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));

     }
     });