0

I displayed data in listview coming from the database and now I want to search particular item from list by provided keyword (ignore case). I tried it but didn't get expected result . Following code I did, it's not giving error but also not fulfill my expectation. Please help to go further.

ArrayList<String> bank=new ArrayList<String>();
EditText inputSearch;
inputSearch.addTextChangedListener(new TextWatcher() {

        private ArrayList<String> arrayList_sort = new ArrayList<String>();

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            int textlength=0;
            textlength=inputSearch.getText().length();
            if(arrayList_sort!=null)
                arrayList_sort.clear();
            for(int i=20;i<=bank.size();i++)
            {
                if(textlength<=bank.get(i).toString().length())
                {
                    //if(inputSearch.getText().toString().equalsIgnoreCase(String) bank.get(i).toString().subSequence(0, textlength)))
                    if (inputSearch
                            .getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) bank.get(i).toString()
                                            .subSequence(0, textlength)))
                    {
                        arrayList_sort.add(bank.get(i));
                        Log.d("TAG", "log" + arrayList_sort.size());
                    }
                }
            }
        }
        public void beforeTextChanged(CharSequence cs, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                          
        }
    });
} 

I used BaseAdapter. It gives error "IndexOutOfBound" when the value of i=0.

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
  • u r not adding the **arrayList_sort** to ur listview..can u show the whole code for clearence? – bakriOnFire May 11 '13 at 05:39
  • First of all, how in thw world can you get `i ==0`, your for loop starts with `i== 20` and its value is nevr decremented. Please post correct code please – CRUSADER May 11 '13 at 05:41
  • http://stackoverflow.com/questions/10816243/search-in-listview-with-edittext/15367403#15367403 – Raghunandan May 11 '13 at 07:07

1 Answers1

0

Don't clear the array list. Preserve it and search the data structure rather than jumping through 3000 hoops by doing getText() and searching through views. Your code will be faster and smaller.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56