在操作栏中实现 SearchView [英] Implementing SearchView in action bar
问题描述
我需要从我的 arrayList
创建 SearchView
并在下拉列表中显示建议
I need to create SearchView
from my arrayList<String>
and show the suggestions in the drop-down list same this
我寻找逐步解释如何在操作栏中构建 SearchView
的教程.
I look for tutorials that explain step by step how to build a SearchView
in a action bar.
我已阅读 文档 并遵循示例 google但它对我没有用.
I have read the documentation and following the example google but it was not useful to me.
我已经创建了搜索
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>`
但是我不知道如何设置字符串数组的参数.我试图在不同的 Activity 中检索结果,但没有成功.
But I do not know how to set the parameters of the array of strings. I tried to retrieve the result in a different Activity but not work.
推荐答案
为此制定解决方案需要一段时间,但我发现这是使其按照您描述的方式工作的最简单方法.可能有更好的方法来做到这一点,但由于您还没有发布您的活动代码,我将不得不即兴发挥并假设您在活动开始时有一个这样的列表:
It took a while to put together a solution for this, but have found this is the easiest way to get it to work in the way that you describe. There could be better ways to do this, but since you haven't posted your activity code I will have to improvise and assume you have a list like this at the start of your activity:
private List<String> items = db.getItems();
ExampleActivity.java
private List<String> items;
private Menu menu;
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.example, menu);
this.menu = menu;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String query) {
loadHistory(query);
return true;
}
});
}
return true;
}
// History
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void loadHistory(String query) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Cursor
String[] columns = new String[] { "_id", "text" };
Object[] temp = new Object[] { 0, "default" };
MatrixCursor cursor = new MatrixCursor(columns);
for(int i = 0; i < items.size(); i++) {
temp[0] = i;
temp[1] = items.get(i);
cursor.addRow(temp);
}
// SearchView
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSuggestionsAdapter(new ExampleAdapter(this, cursor, items));
}
}
现在你需要创建一个从 CursorAdapter
扩展的适配器:
Now you need to create an adapter extended from CursorAdapter
:
ExampleAdapter.java
public class ExampleAdapter extends CursorAdapter {
private List<String> items;
private TextView text;
public ExampleAdapter(Context context, Cursor cursor, List<String> items) {
super(context, cursor, false);
this.items = items;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
text.setText(items.get(cursor.getPosition()));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item, parent, false);
text = (TextView) view.findViewById(R.id.text);
return view;
}
}
一个更好的方法是如果你的列表数据来自数据库,你可以将数据库函数返回的Cursor
直接传递给ExampleAdapter
并使用相关列选择器在适配器中引用的 TextView
中显示列文本.
A better way to do this is if your list data is from a database, you can pass the Cursor
returned by database functions directly to ExampleAdapter
and use the relevant column selector to display the column text in the TextView
referenced in the adapter.
请注意:导入CursorAdapter
时不要导入Android支持版本,而是导入标准的android.widget.CursorAdapter
.
Please note: when you import CursorAdapter
don't import the Android support version, import the standard android.widget.CursorAdapter
instead.
适配器还需要自定义布局:
The adapter will also require a custom layout:
res/layout/item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
您现在可以通过向布局添加额外的文本或图像视图并用适配器中的数据填充它们来自定义列表项.
You can now customize list items by adding additional text or image views to the layout and populating them with data in the adapter.
这应该是全部,但如果您还没有这样做,您需要一个 SearchView 菜单项:
This should be all, but if you haven't done this already you need a SearchView menu item:
res/menu/example.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/search"
android:title="@string/search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
然后创建一个可搜索的配置:
Then create a searchable configuration:
res/xml/searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search"
android:hint="@string/search" >
</searchable>
最后在清单文件的相关活动标签中添加:
Finally add this inside the relevant activity tag in the manifest file:
AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.ExampleActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
请注意:示例中使用的 @string/search
字符串应在 values/strings.xml 中定义,也不要忘记更新对com.example
用于您的项目.
Please note: The @string/search
string used in the examples should be defined in values/strings.xml, also don't forget to update the reference to com.example
for your project.
这篇关于在操作栏中实现 SearchView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!