android – Converting an ArrayAdapter to CursorAdapter for use in a SearchView-ThrowExceptions

Exception or error:

How can I convert an ArrayAdapter<String> of static data into a CursorAdapter for using Suggestion Listener in SearchView?
I have constructed the ArrayAdapter<String> from static data (allString)

ArrayAdapter<String> searchAdapter = new ArrayAdapter<String>(context, R.layout.listitem, allString);

and I use it for an MultiAutoCompleteTextView which works fine in devices with API level less than 11

MultiAutoCompleteTextView findTextView.setAdapter(searchAdapter);

However my target API is level is 11 and for API>10 I use an ActionBar within which I would like to have a SearchView instead.

Here’s what I have tried: It does show the ActionBar with the embedded SearchView but does not give any suggestions as it would in the MultiAutoCompleteTextView.

public boolean onCreateOptionsMenu(Menu menu) {     
        MenuInflater inflater = getMenuInflater();

         if (android.os.Build.VERSION.SDK_INT > 10){
             inflater.inflate(, menu);
             searchView = (SearchView) menu.findItem(;
             int[] to = {0};
             CursorAdapter cursorAdapter = new SimpleCursorAdapter(context, R.layout.listitem, null, allBusStopString, to);
             searchView.setOnSuggestionListener(new OnSuggestionListener() {

                public boolean onSuggestionClick(int position) {
                    String selectedItem = (String)cursorAdapter.getItem(position);
                    Log.v("search view", selectedItem);
                    return false;

                public boolean onSuggestionSelect(int position) {
                    return false;
             inflater.inflate(, menu);

    return true;
How to solve:

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;
String[] from = {"text"}; 
int[] to = {};
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 < /> 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(;
SearchView.SearchAutoComplete searchSrcTextView = (SearchView.SearchAutoComplete) findViewById(;
searchSrcTextView.setAdapter(new SuggestionAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, items));
searchSrcTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

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;

    public long getItemId(int position) {
        return position;

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

    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 {
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

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

            return results;

        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredItems = (List<T>) results.values;
            if (results.count > 0) {
            } else {

Leave a Reply

Your email address will not be published. Required fields are marked *