這個答案比較晚,但正如我所見,其他答案只有答案鏈接。然後,我將嘗試用簡短的示例代碼提供一些解釋。
在Documentation中描述了添加SearchView
,並且很容易按照步驟操作。正如我們在Create a Search Interface主題閱讀:
的<intent-filter>
不需要與DEFAULT值(你通常在<activity>
元素看到)一個<category>
,因爲系統明確地提供了ACTION_SEARCH意圖您的搜索活動,使用其組件名稱。
那麼,你的表現成爲:
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/my_searchable_layout"/>
「傳統上,搜索結果應該在ListView呈現,所以你可能希望你的搜索活動來擴展ListActivity」 - 從文件仍。所以你SearchActivity
可能是:
public class SearchActivity extends ListActivity { }
「當用戶執行從搜索對話框或搜索插件搜索時,系統會創建一個Intent並存儲在它的用戶查詢的系統,然後啓動的活動,你已經聲明處理搜索(「可搜索的活動」),並提供它的意圖「。你需要使用一個Intent
在onCreate
方法從搜索對話框或插件的查詢搜索:
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// Receive the query
String query = intent.getStringExtra(SearchManager.QUERY);
// Search method..
doMySearch(query);
}
doMySearch()
可以是AsyncTask
,新Thread
..連接到SQLite DataBase
,一個SharedPreference
,無論.. 「存儲和搜索數據的過程對您的應用程序來說是獨一無二的」。這就是說,你應該創建一個Adapter
在列表中提供你的結果。
這裏是SearchActivity
使用的AsyncTask來填充列表短ListActivity
例如:
public class SearchActivity extends ListActivity {
// Create an array
String[] values;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity_layout);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// At the end of doMySearch(), you can populate
// a String Array as resultStringArray[] and set the Adapter
doMySearch(query);
}
}
class doMySearch extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... params) {
// Connect to a SQLite DataBase, do some stuff..
// Populate the Array, and return a succeed message
// As String succeed = "Loaded";
return succeed;
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Loaded") {
// You can create and populate an Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
SearchActivity.this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}
}
}
最後,我更喜歡使用SearchView
部件與程序兼容性或ActionBarSherlock,它描述的at the end話題。這是我認爲更適應,因爲你問你的問題(3年前^^)。因此,要做到:
// Example with AppCompat
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu
getMenuInflater().inflate(R.menu.options_menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true); // Iconify the widget
return true;
}
並執行startActivity()
方法傳遞查詢SearchActivity
在onOptionsItemSelected
方法。然後,你就只是爲了這個搜索項添加到您的菜單如下(不要忘了custom prefix)就像你可以看到Adding an Action View:
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
在sample Docs,你有檢索字典演示包含一個ListView
,並通過Adapter
提供連接SQLite DataBase
的完整演示。
Voila!我希望你找到了解決方案,這篇文章將幫助那些想要相同的人。
請停止發佈錯誤/不完整的Android文檔鏈接。它們太多了,連接到通用示例根本沒有幫助。跟指向'http:// developers.android.com'一樣糟糕。最重要的是,我可以想到很多被示例代碼佈局困惑的人。其中不斷變化。 – Sonny 2014-10-21 14:51:01
親愛的@Sonny,我同意最後的Samples文檔,但是此時我沒有找到* Searchable Dictionary *(我稍後會更改它)。但是,*「不正確/不完整」*鏈接?嗯,對不起,但我看不到。正如我所說的,這個答案是:*「用簡短的示例代碼提供一些解釋」* - 不是完整的Android文檔!這些鏈接**與我寫的內容有關。你不覺得這很好嗎?然後,編輯它並建議你的方式! – Fllo 2014-10-21 15:04:34
請接受我的道歉。我會通過編輯來提出我的建議 - 那就是說,現在android網站中沒有這樣的示例。我試圖讓我的搜索工作(2天以上和計數),我收到了答案,我很抱歉,我很粗魯。也就是說,我會說Android的文檔非常好,部分非常混亂。 (例如,一個開始的開發者應該把重點放在API指南還是培訓上?或者兩者兼而有之?它們引導你走向不同的路徑。兩者之間有什麼區別?是否更新文檔以實現錯誤?例如Date/TimePickerDialog。 – Sonny 2014-10-21 15:28:31