在列表中搜索不适用于Android

时间:2012-04-03 13:05:49

标签: android android-listview android-adapter

我有列表视图,其适配器存储为全局变量。我没有通过扩展ListActivity来创建我的列表。我的代码如下:

public void onCreate(Bundle...) {
    mListView = (ListView) findViewById(R.id.listview1);
    //setting my custom array adapter...works fine.
    //also my custom adapter implements Filterable interface
    mListView.setAdapter(...) 
    mListView.setTextFilterEnabled(true);

    //My edit text where I enter my query
    mSearchEditText = (EditText) findViewById(R.id.searchEditText);
    mSearchEditText.addTextChangedLIstener(myListener);
}

private TextWatcher myListener = new TextWatcher () {

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s == null || s.length() == 0) {
            mListView.clearTextFilter();
        }
        else {
            mListView.setFilterText(s.toString());
        }
    }
    ...
    ...
}

我的问题是当我在编辑文本中输入我的字符串时,它在我的列表中给出了正确的可用搜索项目(仅限数字),但不是项目本身。

例如,如果我的列表中有项目 aa,bb,cc,dd,ff,ee,ee

因此,如果我搜索 ee ,我的结果将是 aa,bb ,而不是 ee,ee 。如果我搜索 cc ,我的过滤列表将仅显示 aa

有人可以帮我吗?

2 个答案:

答案 0 :(得分:4)

我有一个有效的例子,试试这个:

filterEditText = (EditText)findViewById(R.id.filter);
filterEditText.addTextChangedListener(filterTextWatcher);

TextWatcher filterTextWatcher = new TextWatcher() {

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

        }  
        public void onTextChanged(CharSequence s, int start, int before,int count) {  
            adapter.getFilter().filter(s);
        }

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

适配器必须implements Filterable

            @Override
            public Filter getFilter() {
//              Filter filter = null;

                if(filter == null)
                    filter = new CheeseFilter();
                return filter;
            }

过滤类:

        public class CheeseFilter extends Filter {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // TODO Auto-generated method stub

                constraint = constraint.toString().toLowerCase();

                FilterResults newFilterResults = new FilterResults();

                if (constraint != null && constraint.length() > 0) {


                    ArrayList<String> auxData = new ArrayList<String>();

                    for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).toLowerCase().contains(constraint))
                            auxData.add(data.get(i));
                    }

                    newFilterResults.count = auxData.size();
                    newFilterResults.values = auxData;
                } else {

                    newFilterResults.count = data.size();
                    newFilterResults.values = data;
                }

                return newFilterResults;
            }

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

                ArrayList<String> resultData = new ArrayList<String>();

                resultData = (ArrayList<String>) results.values;

                EfficientAdapter adapter = new EfficientAdapter(context, resultData);
                list.setAdapter(adapter);

//              notifyDataSetChanged();
            }

        }

您可以查看此帖子了解更多信息:

Filter expandableList

Filter ListView

答案 1 :(得分:0)

配对你需要在适配器上设置它而不是直接在listview上。 因此,请保留您为listview设置的适配器的引用,并在文本更改侦听器中尝试类似的内容:

private TextWatcher myListener = new TextWatcher () 
{

    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {
       myadapter.getFilter().filter(s);
    }
    ...
    ...
}