android studio 中的可搜索 SearchView setSuggestionsAdapter()
Searchable SearchView setSuggestionsAdapter() in android studio
我正在开发一个应用程序。它需要一个搜索视图。我在使用游标适配器时遇到问题。我只是想在建议适配器中显示一个字符串数组或 xml 字符串数组。当我点击搜索按钮时,它会显示建议,而在建议上点击它会完成给定的工作。
我需要代码来帮助我解决这个问题。我不是专业程序员。
您可以使用 SearchAutoComplete 视图,尝试下面这样的操作
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
searchMenuItem = menu.findItem(R.id.action_search)
searchView = (SearchView) searchMenuItem.getActionView();
searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
SearchManager searchManager =
(SearchManager) getSystemService(this.SEARCH_SERVICE);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.suggetion, jsonArrayList);
// jsonarraylist is static string array
searchAutoComplete.setAdapter(adapter);
searchAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Your code for onitemclick
}
}
activity_main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/>
</menu>
suggetion.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/dropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@color/colorAccent"
android:textAppearance="?android:attr/textAppearanceLargePopupMenu"/>
希望对您有所帮助,祝您一切顺利。
我正在开发一个应用程序。它需要一个搜索视图。我在使用游标适配器时遇到问题。我只是想在建议适配器中显示一个字符串数组或 xml 字符串数组。当我点击搜索按钮时,它会显示建议,而在建议上点击它会完成给定的工作。 我需要代码来帮助我解决这个问题。我不是专业程序员。
您可以使用 SearchAutoComplete 视图,尝试下面这样的操作
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
searchMenuItem = menu.findItem(R.id.action_search)
searchView = (SearchView) searchMenuItem.getActionView();
searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
SearchManager searchManager =
(SearchManager) getSystemService(this.SEARCH_SERVICE);
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.suggetion, jsonArrayList);
// jsonarraylist is static string array
searchAutoComplete.setAdapter(adapter);
searchAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Your code for onitemclick
}
}
activity_main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/>
</menu>
suggetion.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/dropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@color/colorAccent"
android:textAppearance="?android:attr/textAppearanceLargePopupMenu"/>
希望对您有所帮助,祝您一切顺利。