android:获取已单击项目的搜索建议文本
我使用的是官方Android示例代码,它为我们提供了一个搜索界面,您可以在其中搜索一个单词,当用户键入时,建议将显示在下拉列表视图中 单击搜索建议时,将向您的可搜索活动发送意图。此意图的操作字段通过可搜索XML进行描述,如下所示:android:获取已单击项目的搜索建议文本,android,search,Android,Search,我使用的是官方Android示例代码,它为我们提供了一个搜索界面,您可以在其中搜索一个单词,当用户键入时,建议将显示在下拉列表视图中 单击搜索建议时,将向您的可搜索活动发送意图。此意图的操作字段通过可搜索XML进行描述,如下所示: <searchable ... android:searchSuggestIntentAction = "android.intent.action.VIEW"> 我的问题是,我的NEWACTIVITY需要一个id来显示相关数据。我可以通过获取
<searchable
...
android:searchSuggestIntentAction = "android.intent.action.VIEW">
我的问题是,我的NEWACTIVITY需要一个id来显示相关数据。我可以通过获取所单击项目的文本(就像我们使用getItemAtPosition(pos);
)获取该id)[字符串值]。我怎么得到它?我做到了
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);
我认为,当用户点击建议时,获取字符串查询的一种安全方法是,在搜索时,从intent android自动传递查询
@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>
快乐编码
这是来自谷歌官方文档:intent.getData()中有什么内容?我不知道,它就在示例项目中。它是否包含所单击项目的值?如果是,那么如何从中提取字符串?我想要的是这样的东西:最终字符串y=(string)txtnum.getText();最终整数x=整数(y)-1的值;myIntent.putExtra(“iid”,x);顺便说一句,您应该真正调用getContentResolver().query(),它以更简单的方式查询您的内容提供者
<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>
@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);
}