我正在使用官方Android示例代码SearchableDictionary,它为我们提供了一个搜索界面,您可以在其中搜索单词,当用户输入时,建议会显示在下拉列表视图中。
单击搜索建议时,会向您的可搜索活动发送意图。此Intent的Action字段通过您可搜索的XML进行描述,如下所示:
<searchable
...
android:searchSuggestIntentAction = "android.intent.action.VIEW">
然后在可搜索的活动中:
Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction()) {
//a suggestion was clicked... do something about it...
Intent myIntent = new Intent(this, NewActivity.class);
myIntent.setData(intent.getData());
startActivity(myIntent);
finish();
}
我的问题是我的NEWACTIVITY需要一个id才能显示相关数据。这个id我可以通过获取被点击的项目的文本来获得(就像我们通过使用getItemAtPosition(pos);
获得的)[字符串值]。我怎么得到它?
答案 0 :(得分:2)
我做到了。
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
cursor.moveToFirst();
String y = cursor.getString(1).trim();
final int x = Integer.valueOf(y) - 1;
myIntent.putExtra("id", y);
答案 1 :(得分:0)
我认为,当用户点击建议时,获取字符串查询的一种安全方式是通过进行搜索时自动从意图android中获取QUERY。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1. Handle the intent on activity startup
handleIntent(getIntent());
}
/**
* This method is necessary if you set android:launchMode="singleTop"
* in your activity Manifest. See the explanation below about singleTop.
* @param query : string the user searched for.
*/
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
// ------------------------------------------------------
// ANSWER IS HERE
// ------------------------------------------------------
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// This will get the String from the clicked suggestion
String query = intent.getStringExtra(SearchManager.QUERY);
displaySearchResults(query);
}
}
/**
* 1. Dismiss the KeyBoard and the SearchView (searchMenuItem.collapseActionView();)
* 2. Switch the container to another fragment;
* 3. Send the query to this new fragment;
*/
private void displaySearchResults(String query) {
dismissKeyBoardAndSearchView();
// Open SearchShowFragment and display the results
SearchShowFragment searchShowFragment = new SearchShowFragment();
Bundle bundle = new Bundle();
bundle.putString(SearchShowFragment.QUERY, query);
searchShowFragment.setArguments(bundle);
switchToFragment(searchShowFragment);
}
建议在活动清单中设置singleTop。因为,如果用户先进行几次搜索,然后再进行几次搜索,则将阻止系统多次创建活动,一次在另一项的顶部。假设用户进行了10次搜索,然后他开始按下“后退”按钮,它将通过所有10个令人烦恼的步骤返回到起点。
<activity android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
观察:我正在使用一个活动(MainActivity)和两个片段, 第一个片段显示所有项目,第二个片段显示项目 通过使用查询搜索。 处理查询,意图,searchview等的逻辑全部在 MainActivity和片段仅接收查询并执行搜索。
用户键入内容时如何处理
请注意,只有当用户点击建议时,这才会引起注意, 当用户键入单词并按Enter时,此功能将无效。
为此,请在onCreateOptionsMenu()中使用以下方法。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflates the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Get the SearchView and set the searchable configuration
final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.menu_search);
searchView = (SearchView) searchMenuItem.getActionView();
// Assumes the current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryRefinementEnabled(true);
// -----------------------------------------------------------------------
// Typing a Query
// -----------------------------------------------------------------------
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// Save the query to display recent queries
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(MainActivity.this,
MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);
// To protect the user's privacy you should always provide a way to clear his search history.
// Put in the menu a way that he can clear the history with the following line below.
/*suggestions.clearHistory();*/
// Display the results
displaySearchResults(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
编码愉快!
这是来自Google的官方文档:Creating a Search Interface