Android listview edittext filter space button?

2020-02-15 06:55发布

I have created a filter listview where user will enter input via edittext and results are filtered in the listview.

The code is running good but when ever i press Space button, results in the list view disappears. how can i add space in search input without dismissing the listview contents ??

Here is my current code :

myListview.setTextFilterEnabled(true);

    edNearBy.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

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

        }

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

            NearByActivity.this.aa.getFilter().filter(s);

        }
    });

2条回答
姐就是有狂的资本
2楼-- · 2020-02-15 07:16

do this way:

public void afterTextChanged(Editable s) {
    String st = s.toString().trim();            
    NearByActivity.this.aa.getFilter().filter(st);
}

but don't do filtering this way, here is the correct one.

查看更多
在下西门庆
3楼-- · 2020-02-15 07:23

I found solution:

String[] hi={"Hi there", "Hello", "Bye"};

adapter = new ArrayAdapter<String>(slownik.this, R.layout.list_item, R.id.word_name,hi);
lv.setAdapter(adapter);

inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        lv.setTextFilterEnabled(true);
        lv.setFilterText(s.toString().trim());
    }

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


    }

    @Override
    public void afterTextChanged(Editable s) {
        slownik.this.adapter.getFilter().filter(s.toString().trim());
        if(s.length()==0){
            lv.clearTextFilter();
        }
    }
});
查看更多
登录 后发表回答