我想在SearchVIEw为空时显示(不同的)建议列表,包括在用户输入任何文本之前.
到目前为止,当用户输入文本然后删除该文本时,它正常工作,当用户点击搜索图标打开小部件时,一旦打开,第二次点击搜索小部件.为此,我使用OnqueryTextListener,如下所示:
class SearchTextEnteredListener implements SearchVIEw.OnqueryTextListener { @OverrIDe public boolean onqueryTextChange(String newText) { if (newText.length() > 1) { // Set normal suggestions CursorAdapter searchadapter = new SearchSuggestionsAdapter(mContext,suggestions); mSearchVIEw.setSuggestionsAdapter(searchadapter); } else if (newText.length() == 1) { // Clear the displayed suggestions if(searchadapter != null && mSearchVIEw != null){ searchadapter.createAndChangeCursor(null); } } else { // Length = 0,show other suggestions CursorAdapter searchadapter = new SearchSuggestionsAdapter(mContext,otherSuggestions); mSearchVIEw.setSuggestionsAdapter(searchadapter); } return false; }}
但是,我希望一旦打开搜索小部件就会d出建议,而不是像现在那样需要第二次点击.我没有运气这样做.
我甚至尝试手动查找SearchVIEw的autoCompleteTextVIEw并在SearchVIEw获得焦点时调用showDropDown()(见下文),但是也没有显示下拉列表.
mSearchVIEw.setonqueryTextFocuschangelistener(new OnFocuschangelistener() { @OverrIDe public voID onFocusChange(VIEw v,boolean hasFocus) { if (hasFocus) { int autoCompleteID = mContext.getResources().getIDentifIEr("androID:ID/search_src_text",null,null); if (autoCompleteID == 0) { // We won't be able to find the vIEw. Give up. return; } autoCompleteTextVIEw autoCompleteVIEw = (autoCompleteTextVIEw) mSearchVIEw.findVIEwByID(autoCompleteID); if (autoCompleteVIEw != null) { autoCompleteVIEw.showDropDown(); } } }});
如何在打开搜索小部件时创建搜索建议下拉列表?
解决方法 有可能的.我正在使用compat v7 SearchVIEw和Cursor Loaders来查询Activity生命周期.// e.g in onCreateVIEw in my Fragment,wire up the SearchVIEw:MenuItem searchMenuItem = toolbar.getMenu().findItem(R.ID.search_menu_item);SearchVIEw searchVIEw = (SearchVIEw) MenuItemCompat.getActionVIEw(searchMenuItem);// and setup Listeners (implementation is shown later)searchVIEw.setonSuggestionListener(this);searchVIEw.setonqueryTextListener(this);searchVIEw.setonCloseListener(this);// then raID the SearchVIEw so search begins on 0 chars!autoCompleteTextVIEw autoCompleteTextVIEw = (autoCompleteTextVIEw) searchVIEw.findVIEwByID(androID.support.v7.appcompat.R.ID.search_src_text);autoCompleteTextVIEw.setThreshold(0);// and set the suggestions adapter so we can use a Loader to do the querIEsSuggestionCursorAdapter suggestionCursorAdapter = new SuggestionCursorAdapter(getContext(),0);searchVIEw.setSuggestionsAdapter(suggestionAdapter);
以下是侦听器的实现:
@OverrIDepublic boolean onqueryTextChange(String userinput) { if (TextUtils.isEmpty(userinput.trim())) { Log.d(TAG,"onqueryTextChange: empty userinput"); // The argument to the loader is null so your query should return all suggestions getLoaderManager().restartLoader(SEARCH_VIEW_LOADER_ID,this); return true; } userinput = userinput.trim(); Bundle args = new Bundle(); args.putString("USER_input",userinput); // The argument contains the users input so your query should use a liKE to find matching suggestions getLoaderManager().restartLoader(SEARCH_VIEW_LOADER_ID,args,this); return true;}@OverrIDepublic boolean onqueryTextsubmit(String query) { return true;}@OverrIDepublic boolean onClose() { return false;}@OverrIDepublic boolean onSuggestionSelect(int position) { return false;}@OverrIDepublic boolean onSuggestionClick(int position) { Cursor c = searchVIEw.getSuggestionsAdapter().getCursor(); final String suggestion = c.getString(c.getColumnIndex("some_column_name")); Log.d(TAG,"onSuggestionClick: position: " + position + " suggestion: " + suggestion); return true;}
另外一个细节,我的SuggestionCursorAdapter扩展了CursorAdaptor并覆盖了以下方法以防止:
java.lang.IllegalStateException:尝试重新打开已经关闭的对象:sqlitequery
@OverrIDepublic voID changeCursor(Cursor newCursor) { newCursor.close();}
锻炼是痛苦所以希望它可以帮助别人!
@H_301_54@ 总结以上是内存溢出为你收集整理的android – 显示空SearchView的建议全部内容,希望文章能够帮你解决android – 显示空SearchView的建议所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)