SearchView和ABS

时间:2013-01-26 15:54:28

标签: android actionbarsherlock

以前我有EditText onTextChangedListener来处理用户搜索查询。这是通过这种方式完成的:

ed.addTextChangedListener(new TextWatcher(){

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

                textLength = ed.getText().length();
                list.clear();
                isPresent = initIsPresentArray();

                for (int i = 0; i < tempList.length; i++) {
                    for (int j = 0; j < (tempList[i].length() - (textLength - 1)); j++) {
                        if (textLength <= tempList[i].length()) {
                            if (isPresent[i] == false) {
                                if (ed.getText()
                                        .toString()
                                        .equalsIgnoreCase(
                                                (String) tempList[i]
                                                        .subSequence(
                                                                j,
                                                                (j + textLength)))) {
                                    list.add(tempCaseList.get(i));
                                    isPresent[i] = true;
                                }
                            }
                        }
                    }
                    if (list.size() == 0) {
                        error.setText(getResources().getString(R.string.notFound));
                        v.setVisibility(View.GONE);
                    } else {
                        error.setText("");
                        v.setVisibility(View.VISIBLE);
                    }

                    lv.setAdapter(new CustomAdapter(History.this, list));
                }
            }
        });

这段令人讨厌的代码会动态更新ListView,同时用户输入要搜索的内容。它与EditText相当不错。现在,我正在为我的项目实现ActionBarSherlock,并希望使用SearchWidget:

enter image description here

我夸大了我的菜单:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.actionbarmenu, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView)menu.findItem(R.id.searchCases).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);
        searchView.setSubmitButtonEnabled(true);
        return true;
    }

如何将OnTextChangedListener的相同行为应用于此SearchView?可能吗?

0 个答案:

没有答案