我正在开发一款应用。它需要一个搜索视图。我在使用游标适配器时遇到问题。我只想在建议适配器中显示一个字符串数组或xml字符串数组。当我点击搜索按钮它显示建议和建议点击它确实给了工作。 我需要的代码可以帮助我解决这个问题。我不是专业的程序员。
答案 0 :(得分:1)
您可以使用 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"/>
我希望这可以帮到你,一切顺利。