问题传递一个捆绑与onSearchRequested [英] Problem passing a Bundle with onSearchRequested
问题描述
我其实想使用Android的内置的搜索界面,但我有一些问题,当我尝试与搜索查询传递数据。
I'm actually trying to use the built-in search interface of Android, but I have some issues when I try to pass data with the search query.
下面是一个简单的解释:我有所谓的类,它实现Serializable第一个活动(FirstActivity)的对象(我已经successfuly传递活动之间的),我想执行与该类别的搜索和显示结果在第二活动(SecondActivity)
Here is a brief explanation : I have an object in a first Activity (FirstActivity) called "Category" which implements Serializable (I already pass it successfuly between Activities) and I want to perform a search related to that category, and display the results in a second Activity (SecondActivity).
所以,我FirstActivity覆盖onSearchRequest方式:
So, in FirstActivity I override the onSearchRequest method :
@Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putSerializable("category", _currentCategory);
Log.d(Utils.LOG_TAG, "Bundle : "+appData.keySet());
startSearch(null, false, appData, false);
return true;
}
而在SecondActivity,我试图让这个软件包:
And in SecondActivity, I try to get this Bundle :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
handleIntent(getIntent());
}
private void handleIntent(Intent intent){
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
if(appData == null) Log.d(Utils.LOG_TAG, "appData == null");
Log.d(Utils.LOG_TAG, "Extras : "+intent.getExtras().keySet());
}
问题是,应用程序数据似乎是等于每次空。这里是logcat的输出:
Problem is that appData seems to be equals to null everytime. Here is the logcat output :
Bundle : [category]
appData == null
Extras : [query, user_query]
我试过一些其他的对象添加到包(布尔值,等...),但它并没有改变任何东西,我一直有一个空的应用程序数据。
I tried to add some other objects into the Bundle (Booleans, etc...) but it doesn't change anything at all and I keep having a null appData.
推荐答案
如果你使用的搜索查看,它不会把你的 APPDATA
。相反,考虑使用 OnQueryTextListener 。例如:
If you're using SearchView, it will not send your appData
. Instead, consider using OnQueryTextListener. For example:
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your-menu-id, menu);
/*
* Get the SearchView and set the searchable configuration.
*/
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.your-search-menuitem-id)
.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
/*
* Set query text listener here.
*/
searchView.setOnQueryTextListener(mSearchViewOnQueryTextListener);
return true;
}// onCreateOptionsMenu()
...
private final SearchView.OnQueryTextListener mSearchViewOnQueryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
/*
* You don't need to deal with "appData", because you already
* have the search query here.
*/
// Tell the SearchView that we handled the query.
return true;
}// onQueryTextSubmit()
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}// onQueryTextChange()
};// mSearchViewOnQueryTextListener
的 注意:的您还需要保留旧的方式(使用 APPDATA
在 onSearchRequested()
)。在你的的onCreate()
,如果额外的 SearchManager.APP_DATA
是空
,这意味着你已经处理的搜索查询的监听器。
Note: You still need to keep the old way (using appData
inside onSearchRequested()
). In your onCreate()
, if the extra for SearchManager.APP_DATA
is null
, that means you already handled the search query in the listener.
结论:的
Conclusion:
- 如果在
搜索查看
是无效的,你通过onSearchRequested调用它()
,会出现这种情况:onSearchRequested()
>>的onCreate()
(ACTION_SEARCH
的包含的SearchManager.APP_DATA
)。 - 如果在
搜索查看
被激活,用户输入并提交搜索,会出现这种情况:SearchView.OnQueryTextListener.onQueryTextSubmit()
>>的onCreate()
(ACTION_SEARCH
的没有的SearchManager.APP_DATA
)。
- If the
SearchView
is inactive, and you invoke it viaonSearchRequested()
, this will happen:onSearchRequested()
>>onCreate()
(ACTION_SEARCH
containsSearchManager.APP_DATA
). - If the
SearchView
is active, the user types and submits search, this will happen:SearchView.OnQueryTextListener.onQueryTextSubmit()
>>onCreate()
(ACTION_SEARCH
withoutSearchManager.APP_DATA
).
这篇关于问题传递一个捆绑与onSearchRequested的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!