Converting an ArrayAdapter to CursorAdapter for use in a SearchView

我怕爱的太早我们不能终老 提交于 2019-11-28 16:25:36
pawelzieba

That's strange SearchView.setSuggestionsAdapter accepts CursorAdapter only.

You could create MatrixCursor and fill it with data from String array. I hope you have small data collection.

Then pass the cursor to CursorAdapter.

String[] columnNames = {"_id","text"}
MatrixCursor cursor = new MatrixCursor(columnNames);
String[] array = getResources().getStringArray(R.array.allStrings); //if strings are in resources
String[] temp = new String[2];
int id = 0;
for(String item : array){
    temp[0] = Integer.toString(id++);
        temp[1] = item;
    cursor.addRow(temp);
}               
String[] from = {"text"}; 
int[] to = {R.id.name_entry};
busStopCursorAdapter = new SimpleCursorAdapter(context, R.layout.listentry, cursor, from, to);

I was facing similar problem. You can use SearchView.setSuggestionsAdapter() which only accepts CursorAdapter. On the other hand... what's the point? If you use the standard <android.support.v7.widget.SearchView /> then it contains SearchAutoComplete which extends AppCompatAutoCompleteTextView inside. The following code worked for me:

List<String> items = Lists.newArrayList(new String[] {"aaaaa", "bbbbb", "ccccc", "ddddd"});
SearchView searchView = (SearchView) findViewById(R.id.autocomplete_searchview);
SearchView.SearchAutoComplete searchSrcTextView = (SearchView.SearchAutoComplete) findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchSrcTextView.setThreshold(1);
searchSrcTextView.setAdapter(new SuggestionAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, items));
searchSrcTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        return;
    }
});

and the following Adapter code:

public class SuggestionAdapter<T> extends ArrayAdapter<T> {

    private List<T> items;
    private List<T> filteredItems;
    private ArrayFilter mFilter;

    public SuggestionAdapter(Context context, @LayoutRes int resource, @NonNull List<T> objects) {
        super(context, resource, Lists.<T>newArrayList());
        this.items = objects;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public T getItem(int position) {
        return items.get(position);
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    public int getCount() {
        //todo: change to pattern-size
        return items.size();
    }

    private class ArrayFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            //custom-filtering of results
            results.values = items;
            results.count = items.size();

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredItems = (List<T>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!