Pergunta

I have created my own adapter that extends BaseAdapter class. Adapters methods are implemented and working fine.

Then I have added Implements Filterable to this class and implemented its method:

@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;
}

Then I added SearchView in my Activity and added to my activity implements OnQueryTextListener

Then I added this SearchView in my menu:

    @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;
}

Then I implement onQueryTextChange method:

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

The dictionaryList is my LisView.

When I start my application and type the text in my device it does nothing! What did I do wrong ?

Foi útil?

Solução

I have found solution. I needed to write:

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

inside of onQueryTextChange

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top