1

Here is my Custom list adapter how can i implement search facility In custom list adapter using edit text. I tried many ways to implement search facilty

     public class ListAdapter extends BaseAdapter  {

   public ArrayList<Users> listDataContainers = new ArrayList<>();

        ListAdapter(ArrayList<Users> listDataContainers) {
            this.listDataContainers = listDataContainers;
        }

        @Override
        public int getCount() {

            return listDataContainers.size();
        }

        @Override
        public User getItem(int arg0) {

            return listDataContainers.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int pos, View arg1, ViewGroup arg2) {

            if (arg1 == null) {
                LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                arg1 = inflater.inflate(R.layout.listitem_layout, arg2, false);
            }
            TextView pa_name = (TextView) arg1.findViewById(R.id.textName);
            TextView pa_date = (TextView) arg1.findViewById(R.id.textRoom);


            Patient item = listDataContainers.get(pos);
            pa_name.setText(item.name);

            pa_date.setText(item.adm_date);

            if (pos % 2 == 0)
                arg1.setBackgroundResource(R.drawable.listview_selector_white);
            else
                arg1.setBackgroundResource(R.drawable.listview_selector_grey);

            return arg1;


        }



}
pcs
  • 1,864
  • 4
  • 25
  • 49
Devidas M Das
  • 478
  • 1
  • 8
  • 28

3 Answers3

5

Or you can use a TextWatcher:

When an object of a type is attached to an Editable, its methods will be called when the text is changed.

    searchField = (EditText) findViewById(R.id.search);
    searchField.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = searchField.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
        }
    });

Now, the EditText will capture user input and pass it to the filter function of your custom adapter class.

In your custom adapter's constructor, you need to initialize a second list, to keep track of the currently desired / searched items:

public class ListAdapter extends BaseAdapter {
public ArrayList<Users> listDataContainers = new ArrayList<>();
private ArrayList<Users> usersFilteredList;
ListAdapter(ArrayList<Users> listDataContainers) {
        this.listDataContainers = listDataContainers;
        this.usersFilteredList = new ArrayList<Users>(); 
        this.usersFilteredList.addAll(listDataContainers);
    }

Add the filter function to your Adapter class and don't forget to call notifyDataSetChanged to update the UI.

public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    listDataContainers.clear();
    if (charText.length() == 0) {
        listDataContainers.addAll(usersFilteredList);
    } else {
        for (Users user : usersFilteredList) {
            if (user.getName().toLowerCase(Locale.getDefault())
                    .contains(charText)) {
                listDataContainers.add(user);
            }
        }
    }
    notifyDataSetChanged();
}

Also, take a look at some other Q&A's on the same topic:

How to use the TextWatcher class in Android?

Community
  • 1
  • 1
appoll
  • 2,910
  • 28
  • 40
  • well of course not, since that is what I assumed you would want to compare against. replace it by any method / field of your Users class – appoll May 05 '15 at 09:45
  • the Users class is the POJO you defined and it should have some private fields/characteristics. Pick the one you want to compare and make a getter method for it – appoll May 05 '15 at 09:47
  • please try to be more articulate, I do not understand what your issue is now. – appoll May 05 '15 at 11:41
  • have you tried debugging? which fields of your Users class are you comparing? if you delete everything from search, does the listview show all elements? – appoll May 05 '15 at 11:45
  • I think I have already spent enough time here. I gave you a possible solution, but it's up to you now to make it work. – appoll May 05 '15 at 11:47
  • if i delete everything from search listview Doesn't show anything – Devidas M Das May 05 '15 at 11:48
1

You need to use OnQueryTextListener, consider searchView to be the edit text:

searchView.setOnQueryTextListener(textChangeListener);

    SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText){

            if (TextUtils.isEmpty(newText)){
                searchView.clearFocus();
                //Search logic when edit text is empty
            }
            else {
                //Search logic when edit text is not empty
            }
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            return true;
        }

    };
Skynet
  • 7,820
  • 5
  • 44
  • 80
0

try this it helps you. use textwatcher for your edittext as below:

// In your main activity

et_Search = (EditText)findViewById(R.id.et_Search);
        et_SearchBar.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) 
            {
                    try 
                    {
                        try{
                        (listDatacontainers).getFilter().filter(s.toString());
                        }
                        catch(Exception e)
                        {
                            Log.i("error at ListAdapter", e.toString());
                        }
                        } 
                    catch (Exception e) 
                    {

                    }

            }

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

            @Override
            public void afterTextChanged(Editable s) 
            {

            }
        });

create method getfilter() in your adapter source file