Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

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

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.

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

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

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

                @Override
                public boolean onSuggestionSelect(int position) {
                    return false;
                }
             });  
         }else{
             inflater.inflate(R.menu.menu, menu);
         }

    return true;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...