Android listview search index from linked exception in fragment

Hi i am working with android. I am trying to implement a list search in my application. I have an edit text and get a value from that edit text and update my list with the base adapter. Now the problem is that it works perfect for 4 character searches. When I enter the 5th character it closes and shows the index from the associated exception. I searched a lot and was guessing the answer "remove getcount". But still it doesn't solve my problem. How can I solve this problem? Thanks in advance:)

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

 }
 }); 

      

here is my cat log

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)

      

here is my adapter class

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

      

+3


source to share


2 answers


I think the problem is with your if condition. Try checking the length of the array inside if condition. try it

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

     }
     });

      



Hope this helps you friend :)

+1


source


Oh, this is a problem with you. Subsequence

method. You seem to be trying to get a match when startsWith

you can use your if this condition

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

      



you can also use the method contains

to get if the search string exits in the target string.

+2


source







All Articles