我的操作栏中有一个 SearchView 小部件,当用户输入一个或多个字符时,它会显示正确的建议搜索词。
我还想在 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;
}
}
但是,我希望搜索小部件一打开就弹出建议,而不是像现在这样需要第二次点击。我没有运气让它这样做。
我什至尝试手动查找 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 queries
SuggestionCursorAdapter suggestionCursorAdapter = new SuggestionCursorAdapter(getContext(), null, 0);
searchView.setSuggestionsAdapter(suggestionAdapter);
监听器的实现如下:
@Override
public 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, null, 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;
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onClose() {
return false;
}
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
@Override
public 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
@Override
public void changeCursor(Cursor newCursor) {
newCursor.close();
}
锻炼很痛苦,希望对大家有帮助!
关于android - 显示针对空 SearchView 的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25870660/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi
如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]
Region是HBase数据管理的基本单位,region有一点像关系型数据的分区。region中存储这用户的真实数据,而为了管理这些数据,HBase使用了RegionSever来管理region。Region的结构hbaseregion的大小设置默认情况下,每个Table起初只有一个Region,随着数据的不断写入,Region会自动进行拆分。刚拆分时,两个子Region都位于当前的RegionServer,但处于负载均衡的考虑,HMaster有可能会将某个Region转移给其他的RegionServer。RegionSplit时机:当1个region中的某个Store下所有StoreFile
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我刚刚按照thebootsygempage上的安装说明进行操作在我保存并查看帖子内容之前,一切看起来都不错。这是输出在View中的样子:HeaderSubhead:似乎没有呈现任何html格式,因为它被引号或类似的东西转义了-其他人有这个问题吗?我没有在github页面或SO上看到任何问题来指出我正确的方向。除了遵循gem安装说明之外,我还没有做任何事情,但也许我错过了什么或者只是犯了一个愚蠢的错误。如果你还有什么想知道的,请尽管问。干杯 最佳答案 你需要有这样的东西,转义html: 关