如何搜索存储在XML文件中的字符串的内容? [英] How to search content of strings stored in XML file?

查看:53
本文介绍了如何搜索存储在XML文件中的字符串的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手.

I'm new to android development.

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

I have an activity containing a bunch of fragments, each fragment shows a different text. I set the text at runtime from strings.xml (i.e tv.setText...)

这是我的strings.xml的示例:

Here is an example of my 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,它将返回整个字符串.

This is my problem:
I want to add search functionality in the app, I want to be able to search for words inside the strings and return the whole string to the user as a result. So for example if the user searches for text2, It would return the whole string.

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

I already read the search guide on android-developers here: http://developer.android.com/guide/topics/search/index.html

我还发现了很多教程,但是它们似乎都处理存储在SQLite数据库中的数据.

I also found a bunch of tutorials, but all of them seem to deal with data stored in SQLite databases.

更多是我的代码:
searchable.xml:

Here is more of my code:
searchable.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>

SearchableActivity:

SearchableActivity:

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)我读过有关使用数据库的信息,但是我不知道如何将所有数据转换为数据库,也不知道如何设置数据库中的文本...等

P.S Is this the best way to do this? I have a lot of strings (>1000) I read about using databases instead but I don't know how to convert all my data into a db, and don't know how to set text from a db...etc

推荐答案

我认为您需要使用一个密钥在strings.xml文件中进行搜索,如果我理解您的意见,那就是答案.

I think you need search in strings.xml file with a key, if I understand you here is the answer .

strings.xml

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

用于在strings.xml中进行搜索的方法

method for searching inside 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

这篇关于如何搜索存储在XML文件中的字符串的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆