How to filter ListView using getFilter() in BaseAdapter

前端 未结 3 1459
长发绾君心
长发绾君心 2020-11-30 09:14

In my application I have create a custom list view and I want to implement a filter so that the list can be filtered according to the text entered in the EditText. I am usin

3条回答
  •  执念已碎
    2020-11-30 10:11

    I find working with filters not all that convenient.. How i do it is:

            ((EditText)findViewById(R.id.etSearch)).addTextChangedListener(new TextWatcher(){
    
            private boolean mCountIncreased;
            @Override
            public void afterTextChanged(Editable s) {
    
                if (s.toString().length() == 0){
                    mDisplayedList.clear();
                    mDisplayedList.addAll(mFullList);
                    mListAdapter.notifyDataSetChanged();
                    return;
                }
    
                if (mCountIncreased){
                    mDisplayedList.clear();
                    mDisplayedList.addAll(mFullList);
                }
    
                List toRemove = new ArrayList();
                for (Item item : mDisplayedList){
                    if (someCondition)
                            toRemove.add(currency);
                    }
                }
    
                mDisplayedList.removeAll(toRemove);
                mListAdapter.notifyDataSetChanged();
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                mCountIncreased = after <= count;
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
    
        });
    }
    

    Note that you'll have to change your adapter to work with the mDisplayedList instead of the mFullList.. and that's it.

    This might give some overhead when your list contains ALOT of entries.. but i've worked like this with a list of +-300 items and i didn't notice anything.

    Hope it helps, Vlad

提交回复
热议问题