我正在创建可搜索的字典,我想将用户在SearchView中执行的最新查询显示给其他活动。就像Android词典中的最近选项一样。
private void handleIntent(Intent inent) {
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
// handles a click on a search suggestion; launches activity to show word
Intent wordIntent = new Intent(this, WordActivity.class);
wordIntent.setData(intent.getData());
startActivity(wordIntent);
finish();
} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// handles a search query
String query = intent.getStringExtra(SearchManager.QUERY);
SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this, MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);
showResults(query);
}
}
ShowResult在搜索活动中显示查询建议,我想将最近的查询显示到其他活动中,我该怎么做?
答案 0 :(得分:0)
这段代码可能会帮助您使用共享偏好。
SharedPreferences sPreferences = getSharedPreferences("tushar", Context.MODE_PRIVATE);
Editor sEditor = sPreferences.edit();
Set<String> setName = new HashSet<String>();
setName.add("tushar");
setName.add("pandey");
setName.add("Ram");
setName.add("Shiva");
sEditor.putStringSet("ko", setName);
sEditor.commit() ;
Log.i("hello","tushar:pandey") ;
Set<String> setName_ = sPreferences.getStringSet("tushar", setName);
Log.i(setName_.toString(),"tushar:pandey") ;
答案 1 :(得分:0)
可以通过ContentResolver访问已保存的查询:
ContentResolver contentResolver = getApplicationContext().getContentResolver();
String contentUri = "content://" + MySuggestionProvider.AUTHORITY + '/' + SearchManager.SUGGEST_URI_PATH_QUERY;
Uri uri = Uri.parse(uriStr);
Cursor cursor = contentResolver.query(uri, null, null, new String[] { null }, null);
现在用接收的光标填充ListView。使用showResults()代码创建另一个活动。