2013-03-10 74 views
0

我是新來的android開發。如何搜索存儲在XML文件中的字符串的內容?

我有一個包含一堆片段的活動,每個片段顯示不同的文本。我設置文本在運行時從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的開發者的搜索指南這裏:http://developer.android.com/guide/topics/search/index.html

我還發現了一堆教程,但他們都似乎處理存儲在SQLite數據庫的數據。

這裏更多的是我的代碼:
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:

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

} 

任何幫助,將不勝感激

PS這是做的最好的方法這個?我有很多字符串(> 1000) 我讀了關於使用數據庫,而不是我如何將所有數據轉換爲數據庫,並不知道如何設置數據庫中的文本...等

回答

0

而不是使用字符串使用字符串數組。在資源創建一個文件並沒有宣佈像代碼以下

<?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 
    } 
} 
+0

不是一個壞主意,但它不是我想到的。 – Reda 2013-03-12 14:39:38

+0

我在一個演示應用程序中嘗試了它,但是我似乎對此方法有一些問題:1 - 如何知道搜索結果的數量。 2 - 如何獲得項目名稱(在數組內)以找到結果。3 - 我設法將結果放在一個自定義listadapter中,但是有沒有辦法配置onclick來打開包含文本的片段(我知道這很難理解,但我需要找到一種更好的在應用程序)。我仍然認爲必須有更好的方式來做所有這些事情,其他人請澄清這個問題。 – Reda 2013-03-12 14:47:24

+0

如果你有非常基本的編程知識,你可以很容易地解決你所指出的問題..所以更好地發展你的編程技巧 – stinepike 2013-03-12 14:55:30

0

我認爲你需要的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