【问题标题】:How to search content of strings stored in XML file?如何搜索存储在 XML 文件中的字符串的内容?
【发布时间】:2013-03-10 11:42:19
【问题描述】:

我是安卓开发新手。

我有一个包含一堆片段的活动,每个片段显示不同的文本。我在运行时从 strings.xml 设置文本(即 tv.setText ...)

这是我的 strings.xml 的示例:

<string name="string1">the content I want searched, text1</string>
<string name="string2">the content I want searched, text2</string>
<string name="string3">the content I want searched, text3</string>

这是我的问题:
我想在应用程序中添加搜索功能,我希望能够在字符串中搜索单词并将整个字符串作为结果返回给用户。 因此,例如,如果用户搜索 text2,它将返回整个字符串。

我已经在这里阅读了关于 android-developers 的搜索指南:http://developer.android.com/guide/topics/search/index.html

我还找到了一堆教程,但似乎都是处理存储在 SQLite 数据库中的数据。

这是我的更多代码:
可搜索的.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search" >
</searchable>

可搜索活动:

public class SearchableActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

        handleIntent(getIntent());
    }


    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }



    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doMySearch(query);
        }
    }

    private void doMySearch(String query) {
    }

}

任何帮助将不胜感激

P.S 这是最好的方法吗?我有很多字符串(>1000) 我阅读了有关使用数据库的信息,但我不知道如何将所有数据转换为数据库,也不知道如何从数据库中设置文本...等

【问题讨论】:

    标签: java android eclipse search


    【解决方案1】:

    我认为您需要在 strings.xml 文件中使用密钥进行搜索,如果我理解您,这就是答案。

    strings.xml

    <string name="string1">the content I want searched, text1</string>
    

    strings.xml 中的搜索方法

    private String SearchForString(String message){
    // get the resource id if matched any key in strings 
    // message Passed string you want search for
    // "string" type of what you looking for
    // package name
    
    try {
        int resId = getResources().getIdentifier(message , "string" , getPackageName());
        String stringReturned =  getResources().getString(resId);
    return stringReturned;
      } catch(Exception e){
      return null;
      }
      }
    

    立即调用方法

    SearchForString("string1");
    

    它应该返回:我要搜索的内容,text1

    【讨论】:

    • 方法名最好以小写开头,例如searchForString()
    • 我尝试了您的解决方案,但它的作用基本上是为您提供与您在 searchForString() 参数中发送的 id 对应的字符串的值。它没有做任何特别的事情。
    【解决方案2】:

    不要使用字符串,而是使用字符串数组。在资源中创建一个文件并声明如下

    <?xml version="1.0" encoding="utf-8"?><resources>
    <string-array name="names">
        <item>the content I want searched, text1</item>
        <item>the content I want searched, text2</item>
        <item>the content I want searched, text3</item>
    </string-array>  
    

    现在在代码中执行以下操作来搜索字符串

    String[] names = getResources().getStringArray(R.array.names);
    for (String s : names) {
        int i = s.indexOf(searchKeyword);
        if (i >= 0) {
            // found a match
        }
    }
    

    【讨论】:

    • 我在一个demo app里试过了,但是这个方法好像有点问题: 1-我怎么知道找到的结果个数。 2-如何获取找到的结果的项目名称(在数组内)。 3-我设法将结果放在自定义列表适配器中,但是有没有办法配置 onclick 以打开包含文本的片段(我知道这很牵强,但我需要找到一种更好的方法来实现搜索应用程序)。我仍然认为必须有更好的方法来完成所有这些工作,请其他人解释一下这个问题。
    • 如果你有非常基本的编程知识,你可以很容易地解决你注意到的问题..所以更好地提高你的编程技能
    • @stinepike 但是如果我们无法将string 转换为string-array 怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2014-03-18
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多