我想在我的应用中实现搜索,但我不想使用单独的活动来显示我的搜索结果。相反,我只想使用显示在SearchView
下的建议列表。使用SearchView进行自定义搜索
我可以在SearchView
上使用setOnQueryTextListener
,监听输入并搜索结果。但是,如何将这些结果添加到SearchView
以下的列表中?假设我在List<String>
中搜索。
我想在我的应用中实现搜索,但我不想使用单独的活动来显示我的搜索结果。相反,我只想使用显示在SearchView
下的建议列表。使用SearchView进行自定义搜索
我可以在SearchView
上使用setOnQueryTextListener
,监听输入并搜索结果。但是,如何将这些结果添加到SearchView
以下的列表中?假设我在List<String>
中搜索。
你需要创建的是一个Content Provider。 通过这种方式,您可以将自定义结果添加到SearchView,并在用户输入内容时向其添加自动完成功能。
如果我没有记错的话,在我的一个项目中,我做了类似的事情,而且没有太长时间。
我认为这可能是有益的:Turn AutoCompleteTextView into a SearchView in ActionBar instead
而且也是这样:SearchManager - adding custom suggestions
希望这有助于。
N.
是否可以在活动的同一个操作栏中添加两个搜索小部件? –
我以一个EditText这需要搜索字符串实现的搜索我的应用程序。
而在这个EditText下面我有我想要执行搜索的ListView。
<EditText
android:id="@+id/searchInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/input_patch"
android:gravity="center_vertical"
android:hint="@string/search_text"
android:lines="1"
android:textColor="@android:color/white"
android:textSize="16sp" >
</EditText>
<ListView
android:id="@+id/appsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/searchInput"
android:cacheColorHint="#00000000" >
</ListView>
搜索EditText下面的列表根据在EditText中输入的搜索文本而改变。
etSearch = (EditText) findViewById(R.id.searchInput);
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
searchList();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
功能searchList()做实际的搜索
private void searchList() {
String s = etSearch.getText().toString();
int textlength = s.length();
String sApp;
ArrayList<String> appsListSort = new ArrayList<String>();
int appSize = list.size();
for (int i = 0; i < appSize; i++) {
sApp = list.get(i);
if (textlength <= sApp.length()) {
if (s.equalsIgnoreCase((String) sApp.subSequence(0, textlength))) {
appsListSort.add(list.get(i));
}
}
}
list.clear();
for (int j = 0; j < appsListSort.size(); j++) {
list.add(appsListSort.get(j));
}
adapter.notifyDataSetChanged();
}
这里list
是显示在ListView和adapter
是ListView的适配器的ArrayList。
我希望这能以某种方式帮助你。
你能否提供一个示例代码片段,就像你如何实现它一样? – Anirudh