Android listview edittext过滤空间按钮?

时间:2012-01-04 06:09:49

标签: android listview android-edittext

我创建了一个过滤列表视图,用户将通过edittext输入输入,并在列表视图中过滤结果。

代码运行良好,但是当我按下Space按钮时,列表视图中的结果将消失。如何在不忽略listview内容的情况下在搜索输入中添加空间?

这是我目前的代码:

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 个答案:

答案 0 :(得分:3)

这样做:

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

但不要以这种方式进行过滤,here is the correct one

答案 1 :(得分:1)

我找到了解决方案:

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();
        }
    }
});