当按下Backspace时,Android addTextChangedListener onTextChanged没有被触发?

时间:2013-10-28 01:23:54

标签: android android-listview android-filterable android-filter

我使用addTextChangeListener来过滤列表项。当用户在editText上输入字符时,将根据用户输入过滤项目。例如,如果用户输入“stackoverflow”,则显示包含“stackoverflow”的所有项目。它工作正常,但一旦用户按退格键删除字符,项目就不会被删除,直到他删除所有字符。

例如,我的项目是:“stackoverflow 1”,“stackoverflow 2”,“stackoverflow 3”,“stackoverflow 4”。

如果用户输入为“stackoverflow”,则显示所有项目。如果用户输入为“stackoverflow 1”,则仅显示“stackoverflow 1”。然后用户删除最后2个字符(1和空格)。用户输入现在是“stackoverflow”,但仍然显示“stackoverflow 1”,并且不显示其他项目。

这是我的自定义过滤器:

private class ServiceFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                // No filter implemented we return all the list
                results.values = origServiceList;
                results.count = origServiceList.size();
            }
            else {
                List<ServiceModel> nServiceList = new ArrayList<ServiceModel>();
                for (ServiceModel s : serviceList) {
                    if (s.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        nServiceList.add(s);
                    }
                }
                results.values = nServiceList;
                results.count = nServiceList.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            if (results.count == 0) {
                notifyDataSetInvalidated();
            }
            else {
                serviceList = (List<ServiceModel>) results.values;
                notifyDataSetChanged();
            }
        }   
    }

我如何处理文字更改事件:

inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }

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

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

                if (cs.length() >= 1) {
                    mAdapter.getFilter().filter(cs);
                }
                else {
                    mAdapter = new ServiceAdapter(MyActivity.this, R.layout.ussd_list_item, serviceList);
                    listView.setAdapter(mAdapter);
                }
            }

        });

1 个答案:

答案 0 :(得分:1)

我会说,因为您过滤了现有的过滤结果,而不是原始结果。因此,当您开始退格时,它只会在您之前的结果中进行搜索。

试试看它是否修复了它:

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        FilterResults results = new FilterResults();
        if (constraint == null || constraint.length() == 0) {
            // No filter implemented we return all the list
            results.values = origServiceList;
            results.count = origServiceList.size();
        }
        else {
            List<ServiceModel> nServiceList = new ArrayList<ServiceModel>();
            for (ServiceModel s : origServiceList) {
                if (s.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                    nServiceList.add(s);
                }
            }
            results.values = nServiceList;
            results.count = nServiceList.size();
        }
        return results;
    }