I am using a SearchView in the Action Bar. I want to use autocomplete feature on the search view to get results from the database.
Is this possible? Or do I need to
Here's an alternative method using CursorAdapter:
ExampleActivity.java
private Menu menu;
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.example, menu);
this.menu = menu;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String query) {
loadHistory(query);
return true;
}
});
}
return true;
}
// History
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void loadHistory(String query) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Cursor cursor = db.rawQuery("SELECT * FROM items", null); // Example database query
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setSuggestionsAdapter(new ExampleAdapter(this, cursor, items));
}
}
Now you need to create an adapter extended from CursorAdapter:
ExampleAdapter.java
public class ExampleAdapter extends CursorAdapter {
private List items;
private TextView text;
public ExampleAdapter(Context context, Cursor cursor, List items) {
super(context, cursor, false);
this.items = items;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
text.setText(cursor.getString(cursor.getColumnIndex("text"))); // Example column index
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item, parent, false);
text = (TextView) view.findViewById(R.id.text);
return view;
}
}
Please note: when you import CursorAdapter don't import the Android support version, import the standard android.widget.CursorAdapter instead.
The adapter will also require a custom layout:
res/layout/item.xml
You can now customize list items by adding additional text or image views to the layout and populating them with data in the adapter. Now you need a SearchView menu item:
res/menu/example.xml
Then create a searchable configuration:
res/xml/searchable.xml
Finally add this inside the relevant activity tag in the manifest file:
AndroidManifest.xml
Please note: The @string/search string used in the examples should be defined in values/strings.xml, also don't forget to update the reference to com.example for your project.
Here's the original tutorial for reference:
http://tpbapp.com/android-development/android-action-bar-searchview-tutorial