过滤器不适用于空间android

时间:2012-11-06 06:37:58

标签: android android-layout

嗨我有一个编辑框,如果用户输入过滤应该在listview中自动完成...

从下面的代码全部工作正常,但如果我在字符中放置空格列表视图不显示任何条目..

下面是我的代码

      adapter = new SimpleAdapter(PredefinedLocation.this, fillMaps, R.layout.list_item_deal, new String[] { "name" }, new int[] { R.id.name });
    // Adding data into listview
    lv.setAdapter(adapter);
    edtSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            lv.setTextFilterEnabled(true);
            lv.setFilterText(s.toString().trim());
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if(s.length()==0){
                lv.clearTextFilter();
            }
        }
    });

如果我写“The”,结果将是“The Local”,“The Adams”

但如果我写“本地”,结果将为空白....因为“The”中的空格“本地”:(

1 个答案:

答案 0 :(得分:1)

出现此问题是因为您使用 SimpleAdpater ,尝试使用 ArrayAdapter 它会解决您的问题。我不知道为什么会这样!

看看这个

http://www.talkandroid.com/android-forums/android-development-answers-tutorials-code-snippets/2534-diferent-filter-behavior-simpleadapter-arrayadapter.html

尝试以这种方式放置代码,我已经测试了它的工作原理

  adapter = new ArrayAdapter<String>(PredefinedLocation.this, R.layout.list_item_deal, R.id.name, fillMaps);

    lv.setAdapter(adapter);
    edtSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            lv.setTextFilterEnabled(true);
            lv.setFilterText(s.toString().trim());
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            PredefinedLocation.this.adapter.getFilter().filter(s.toString().trim());
            /*if(s.length()==0){
                lv.clearTextFilter();
            }*/
        }
    });