Converting an ArrayAdapter to CursorAdapter for use in a SearchView

后端 未结 2 1302
终归单人心
终归单人心 2020-12-12 20:01

How can I convert an ArrayAdapter of static data into a CursorAdapter for using Suggestion Listener in SearchView? I h

2条回答
  •  长情又很酷
    2020-12-12 20:30

    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);
    

提交回复
热议问题