提案機能を備えたActionBar検索ウィジェットを実装したい。
既に持っている ORMLiteデータベースに保存されている、提案に使用したい文字列配列。
クラス(プロバイダー、検索可能...!)を大量に作成せずにこれを行うにはどうすればよいですか?
これはサンプルであり、お役に立てば幸いです。
このカーソルにより多くの機能を実装するには、例えば提案項目を押したときにアクティビティを開くには、これを見てください http://developer.Android.com/guide/topics/search/adding-custom-suggestions.html
package com.roberto.Android.thetracker.app;
import Android.database.MatrixCursor;
import Android.os.Bundle;
import Android.provider.BaseColumns;
import Android.support.v4.app.Fragment;
import Android.support.v4.view.MenuItemCompat;
import Android.support.v4.widget.CursorAdapter;
import Android.support.v4.widget.SimpleCursorAdapter;
import Android.support.v7.widget.SearchView;
import Android.view.Menu;
import Android.view.MenuInflater;
public class CustomSearchFragment extends Fragment {
private static final String[] SUGGESTIONS = {
"Bauru", "Sao Paulo", "Rio de Janeiro",
"Bahia", "Mato Grosso", "Minas Gerais",
"Tocantins", "Rio Grande do Sul"
};
private SimpleCursorAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String[] from = new String[] {"cityName"};
final int[] to = new int[] {Android.R.id.text1};
mAdapter = new SimpleCursorAdapter(getActivity(),
Android.R.layout.simple_list_item_1,
null,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(menu.findItem(R.id.action_search));
searchView.setSuggestionsAdapter(mAdapter);
searchView.setIconifiedByDefault(false);
// Getting selected (clicked) item suggestion
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {
Cursor cursor = (Cursor) mAdapter.getItem(position);
String txt = cursor.getString(cursor.getColumnIndex("cityName"));
searchview.setQuery(txt, true);
return true;
}
@Override
public boolean onSuggestionSelect(int position) {
// Your code here
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
populateAdapter(s);
return false;
}
});
}
// You must implements your logic to get data using OrmLite
private void populateAdapter(String query) {
final MatrixCursor c = new MatrixCursor(new String[]{ BaseColumns._ID, "cityName" });
for (int i=0; i<SUGGESTIONS.length; i++) {
if (SUGGESTIONS[i].toLowerCase().startsWith(query.toLowerCase()))
c.addRow(new Object[] {i, SUGGESTIONS[i]});
}
mAdapter.changeCursor(c);
}
}
betorcs の回答:に基づく
持っている必要はありません
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {...});
代わりにこれを使用できます
mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
return populateAdapter((String) constraint);
}
});
populateAdapterで、「return c;」を置き換えます。 「mAdapter.changeCursor(c);」
private Cursor populateAdapter(String query) {
.
.
.
return c;
}
private String getSuggestion(int position) {
MatrixCursor cursor = (MatrixCursor) searchView.getSuggestionsAdapter().getCursor();
String suggest1 = cursor.getString(position);
return SUGGESTIONS[Integer.valueOf(suggest1)];
}