Android listview 适配器不过滤



我创建了自己的适配器来扩展BaseAdapter类。 适配器方法已实现并工作正常。

然后我Implements Filterable添加到这个类中并实现了它的方法:

@Override
public Filter getFilter() {
    Filter filter = new Filter() {
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // has the filtered values
            elements = (ArrayList<DictionaryListElement>) results.values;
            // notifies the data with new filtered values. Only filtered
            // values will be shown on the list
            notifyDataSetChanged();
        }
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            Locale locale = new Locale("KK");
            if (mOriginalValues == null) {
                mOriginalValues = new ArrayList<DictionaryListElement>(elements); // saves                                                                          // mOriginalValues
            }
            if (constraint == null || constraint.length() == 0) {
                /* CONTRACT FOR IMPLEMENTING FILTER : set the Original values to result which will be returned for publishing */
                results.count = mOriginalValues.size();
                results.values = mOriginalValues;
            } else {
                ArrayList<DictionaryListElement> filteredArrayList = new ArrayList<DictionaryListElement>();
                String searchword = constraint.toString();
                searchword = searchword.toLowerCase(locale);

                for (int i = 0; i < mOriginalValues.size(); i++) {
                    String word = mOriginalValues.get(i).getWord();
                    if (word.toLowerCase(locale).startsWith(searchword)) {
                        filteredArrayList.add(mOriginalValues.get(i));
                    }
                }
                // set the Filtered result to return
                results.count = filteredArrayList.size();
                results.values = filteredArrayList;
            }
            return results;
        }
    };
    return filter;
}

然后,我在"活动"中添加了SearchView,并添加到"活动"implements OnQueryTextListener

然后我在菜单中添加了这个SearchView

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    search = new SearchView(getActionBar().getThemedContext());
    search.setIconified(false);
    search.setSubmitButtonEnabled(false);
    search.setOnQueryTextListener(this);
    search.setQueryHint(getString(R.string.enter_the_word));
    menu.add(0, 1, 1, null)
            .setIcon(android.R.drawable.ic_search_category_default)
            .setActionView(search)
            .setShowAsAction(
                    MenuItem.SHOW_AS_ACTION_IF_ROOM
                            | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    return true;
}

然后我实现onQueryTextChange方法:

@Override
public boolean onQueryTextChange(String newText) {
    // TODO Auto-generated method stub
    if (TextUtils.isEmpty(newText)) {
        dictionaryList.clearTextFilter();
    } else {
        dictionaryList.setFilterText(newText);
    }
    return true;
} 

dictionaryList是我的LisView.

当我启动应用程序并在设备中键入文本时,它什么也没做!我做错了什么?

我已经找到了解决方案。我需要写:

if (TextUtils.isEmpty(newText)) {
        dictionaryList.clearTextFilter();
    } else {
        //dictionaryList.setFilterText(newText);
        dictionaryList.setSelectionAfterHeaderView();
        DictionaryListAdapter adapter = (DictionaryListAdapter) dictionaryList.getAdapter();
        adapter.getFilter().filter(newText);
    }

onQueryTextChange内部

相关内容

  • 没有找到相关文章

最新更新