Java Android:提供最近的搜索建议,但没有可搜索的活动?
在 用户brandall发布了以下解决方案(在帖子底部):Java Android:提供最近的搜索建议,但没有可搜索的活动?,java,android,xml,android-actionbar,searchview,Java,Android,Xml,Android Actionbar,Searchview,在 用户brandall发布了以下解决方案(在帖子底部): import java.util.ArrayList; 导入android.app.Activity; 导入android.app.SearchManager; 导入android.content.Context; 导入android.database.Cursor; 导入android.database.MatrixCursor; 导入android.os.Bundle; 导入android.view.LayoutInflater;
import java.util.ArrayList;
导入android.app.Activity;
导入android.app.SearchManager;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.MatrixCursor;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.CursorAdapter;
导入android.widget.SearchView;
导入android.widget.SearchView.OnQueryTextListener;
导入android.widget.TextView;
导入android.widget.Toast;
公共类ActivityTest在QueryTextListener上扩展活动实现{
私有静态最终字符串列_ID=“_ID”;
私有静态最终字符串列\u TERM=“TERM”;
私有静态最终字符串DEFAULT=“DEFAULT”;
私人搜索管理器搜索管理器;
私有搜索视图搜索视图;
私有菜单项搜索菜单项;
私有建议适配器建议适配器;
private final ArrayList SuggestionArray=新ArrayList();
私有最终ArrayList dummyArray=新ArrayList();
@凌驾
创建时受保护的void(最终捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建一些虚拟条目
添加(“苹果”);
添加(“橙子”);
添加(“香蕉”);
添加(“梨”);
添加(“李子”);
}
@凌驾
公共布尔onCreateOptions菜单(最终菜单){
getMenuInflater().充气(R.menu.main,menu);
searchManager=(searchManager)getSystemService(Context.SEARCH\u服务);
searchMenuItem=menu.findItem(R.id.action\u search);
searchView=(searchView)searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName());
setOnQueryTextListener(这个);
最终MatrixCursor MatrixCursor=getCursor(SuggestionArray);
SuggestionAdapter=新的SuggestionAdapter(this、matrixCursor、SuggestionArray);
searchView.setSuggestionAdapter(SuggestionAdapter);
suggestionsAdapter.notifyDataSetChanged();
返回true;
}
@凌驾
公共布尔onQueryTextChange(最终字符串newText){
suggestionsArray.clear();
对于(int i=0;i
作为一个完全的新手,适当的XML资源文件是什么样子的
谢谢
目前我有以下res文件:
布局文件夹:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ActivityTest">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
activity\u activity\u test.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ActivityTest">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
活动\u main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ActivityTest">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
list\u item\u search.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ActivityTest">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>