java - 如何访问自定义搜索建议提供程序中的本地化字符串?

标签 java android android-resources

我的应用程序的 SearchView 有一个自定义搜索建议提供程序。这些建议显示得很好,但建议名称是硬编码的。我想从我的 strings.xml 文件中获取本地化字符串,但我需要访问我的搜索提供程序中没有的 Activity 上下文。

按照 Android 开发者文档中的建议,我的搜索提供程序使用 searchables.xml 文件映射到我的 SearchView。这是我的可搜索配置:

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

}

我想从我的字符串资源文件中获取“植物”和“鸟类”等术语的本地化字符串。

我该怎么做?

谢谢。

最佳答案

尝试类似:

字符串值 = this.getContext().getResources().getString(R.string.Plants)

关于java - 如何访问自定义搜索建议提供程序中的本地化字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12370304/

相关文章:

java - lambda函数如何成为Comparator的compare()方法

android - 使用名称中的变量从 values/colors.xml 中检索颜色(R.color.name + 变量)

java - 前缀字符串资源标识符以在运行时选择替代版本

android - jquery ajax GET请求执行两次

Android:以编程方式 setId 给出警告 Expected resource of type id

java - 在 PHP 代码中运行 Java 程序

java - 在 Play 框架中使用 Files.createSymbolicLink 获取 FileSystemException "A required privilege is not held by the client"

java - Spring - java.io.FileNotFoundException

java - Android - Java - Buttonclicker nullpointerException

android,用模拟器录制音频