Android: addTextChangedListener not working well

不问归期 提交于 2019-12-05 19:15:45

From your code, What I could understand is, you want to filter the ListView.

Instead of doing filter by yourself you should use listView.setFilterText(String).

Like this way:

add your adapter for first and one time.

lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this,
                    android.R.layout.simple_list_item_multiple_choice, your_array_contents));

and then add TextWatcher:

txtFilter.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {
        if(s.length()==0){
            lv.clearTextFilter();
        }
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){
    }
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        lv.setTextFilterEnabled(true);
        lv.setFilterText(s.toString());
    }
});

Try this way

inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text

            MainActivity.this.adapter.getFilter().filter(cs.toString());
        }

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

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!