1

I have to add search functionality on Edittext items in listview are coming from baseadapter I am using this code but .getFilter().filter(s.toString()); is not coming

My Code is:

approvedfriendList.setAdapter(new ApprovedList());

            //*********Search Functionality in Approved Friend List************************
            serch_item.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub
                //  approvedfriendList.getFilterTouchesWhenObscured().f
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

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

                }
            });
    approvedfriendList is ArrayList 

I am not getting how to use here .getFilter().filter(s.toString()); with ApprovedList()(Base Adapter Class )

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 3
    http://stackoverflow.com/questions/10816243/search-in-listview-with-edittext/15367403#15367403. check the link for custom search. http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/ search in listview – Raghunandan May 14 '13 at 06:43

2 Answers2

0

First you should enable your list view for filtering by adding this line of code :

list.setTextFilterEnabled(true);

then simply add a text watcher in your text view :

text.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
              adapter.getFilter().filter( s.toString());
              list.setAdapter(adapter);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable prefix) { 
        }       

    });
Chirag Jain
  • 1,612
  • 13
  • 20
  • I get an error and my app FC. http://stackoverflow.com/questions/20524417/listview-is-blank-while-using-getfilter-function – Si8 Dec 13 '13 at 17:26
0

I implemented a search of contacts in my app. Followimg is a part of the code:

searchContacts.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                temp.clear();
                for (int i = 0; i < emailIds.size(); i++) {
                    if (emailIds.get(i).toLowerCase().startsWith(s.toString())) {
                        temp.add(emailIds.get(i));
                    }
                }
                Collections.sort(temp);
                contacts.notifyDataSetChanged();

                for (int i = 0; i < temp.size(); i++) {
                    if (sqlHandler.isChecked(temp.get(i))) {
                        lvContacts.setItemChecked(i, true);
                    } else {
                        lvContacts.setItemChecked(i, false);
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
Lincy
  • 1,033
  • 1
  • 8
  • 14