アプリケーションのカスタム検索候補プロバイダーがありますSearchView
。提案は問題なく表示されますが、提案名はハードコーディングされています。ファイルからローカライズされた文字列をフェッチしたいのstrings.xml
ですが、検索プロバイダーにないアクティビティコンテキストにアクセスする必要があります。
私の検索プロバイダーは、Androidデベロッパードキュメントで推奨されているファイルをSearchView
使用してマップされています。searchables.xml
これが私の検索可能な構成です:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="artist, track informations..."
android:label="@string/app_name"
android:queryAfterZeroResults="true"
android:searchSuggestAuthority="com.example.testapp.providers.QuickSearchProvider"
android:searchSuggestSelection=" ?"
android:searchSuggestThreshold="3" />
...実際の検索プロバイダーは次のとおりです。
public class QuickSearchProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "com.example.testapp.providers.QuickSearchProvider";
public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;
public QuickSearchProvider() {
setupSuggestions(AUTHORITY, MODE);
}
public Cursor query(Uri uri, String[] projection, String sel,
String[] selArgs, String sortOrder) {
MatrixCursor cursor = new MatrixCursor(new String[] {
BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_ICON_1,
SearchManager.SUGGEST_COLUMN_INTENT_ACTION});
cursor.addRow(new Object[] { 0, "Plants", "Search Plants", android.R.drawable.ic_menu_search, Search.SEARCH_PLANTS_ACTION});
cursor.addRow(new Object[] { 1, "Birds", "Search Birds", android.R.drawable.ic_menu_search, Search.SEARCH_BIRDS_ACTION });
return new MergeCursor(new Cursor[] { cursor });
}
}
「Plants」や「Birds」などの用語について、文字列リソースファイルからローカライズされた文字列を取得したいと思います。
これどうやってするの?
ありがとう。