我尝试按照指南here进行操作,但我遇到了一些问题。我正确地将搜索图标显示在操作栏中,但是我无法将searchable configuration
附加到SearchView。
我的res / menu / options_menu.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
我的res / xml / searchable.xml文件(注意:我必须在res文件夹中创建一个“xml”文件夹,因为一个文件夹不存在。我不认为这会有所作为吗?
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />
我的res / values / strings.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Starting To Feel It</string>
<string name="single_post">Single Post View</string>
<string name="search">Search Activity</string>
<string name="action_settings">Settings</string>
<string name = "pagination_last">Last</string>
<string name = "pagination_next">Next</string>
<string name = "player_play">Play</string>
<string name = "player_previous">Previous</string>
<string name = "player_next">Next</string>
<string name = "player_playlist">Playlist</string>
<string name = "player_progress_bar">Progress Bar</string>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
<string name="search_title">Search STFI</string>
<string name="search_hint">Search STFI</string>
<string name="menu_item_picture">Menu Item Picture</string>
</resources>
我的AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stfi"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@android:style/Theme.Holo.Light" >
<activity
android:name=".StartingToFeelIt"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.SinglePost"
android:label="@string/single_post">
</activity>
<activity
android:name=".activities.SearchActivity"
android:label="@string/search">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
</manifest>
我的所有活动都扩展了一个名为MenuActivity
的类,这里是MenuActivity.java中定义的onCreateOptionsMenu
函数:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
System.out.println("The component name is " + this.getComponentName().toString());
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
if(info == null)
{
System.out.println("It is null!");
}
searchView.setSearchableInfo(info);
return true;
}
我总是在这个函数中获取该信息为null。我不确定我做错了什么来获取信息是空的,我不知道如何解决它。我不确定你是否需要它,但下面是我的SearchActivity类。现在它没有做任何事情,只是尝试显示搜索(但是,它从未被调用,因为搜索配置未正确设置)。该文件保存在com.stfi.activities.SearchActivity.java中。
另一个注意事项:当我在上面的函数中打印出组件名称时,我得到的是The component name is ComponentInfo(com.stfi/com.stfi.StartingToFeelIt)
,而不仅仅是com.stfi.StartingToFeelIt。这让我相信我的AndroidManifest文件有问题,但我不知道这是不是我无法附加搜索配置的原因。
public class SearchActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
System.out.println("You are searchin " + query);
}
}
}
答案 0 :(得分:2)
我已经四处寻找并找到了你需要做的两件事。
1。更新您的Manifest.xml并使用<meta
添加您的搜索界面。
2. 为了使其具有可搜索的配置,您需要添加元数据
<activity ... >
...
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
答案 1 :(得分:1)
我猜这个清单可能有问题,
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
您的清单的“StartingToFeelIt”的活动代码中缺少
<activity
android:name=".StartingToFeelIt"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
您也可以使用ABS CollapsibleSearchMenu Lib。
答案 2 :(得分:1)
<activity
android:name="com.yourapp.YourSearchActivity"
android:label="@string/title_activity_search"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
在我的活动菜单XML中:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.mirrorcamera.MainActivity" >
<item
android:id="@+id/search"
android:showAsAction="always"
android:title="@string/string_title"/>
</menu>
Java代码:
public class SearchActivity extends ListActivity {
private static final int ICON = R.drawable.ic_ab_back_holo_light;
private SearchDataBaseAdapter searchDataBaseAdapter;
private ArrayList<SearchResultModel> searchResultList;
private ResultListAdapter resultListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
super.onCreate(savedInstanceState);
searchResultList = new ArrayList<SearchResultModel>();
// gets the db reference from adapter
searchDataBaseAdapter = new SearchDataBaseAdapter(this);
searchDataBaseAdapter.open();
handleIntent(getIntent());
}
/**
* Handles the intent from the search.
*
* @param intent
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// handles a search query
String query = intent.getStringExtra(SearchManager.QUERY);
search(query);
}
}
@Override
public void onNewIntent(Intent intent) {
// super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
break;
}
return true;
}
/**
* Search function.
*
* @param searchTerm
*/
public void search(String searchTerm) {
try {
Cursor cursor = searchDataBaseAdapter.getWordMatches(searchTerm, null);
//create and add your list to your adapter.
resultListAdapter = new ResultListAdapter(this, itemsFound(cursor, cursor.getString(cursor.getColumnIndex("YOUR_COLUMN"))));
} catch (NullPointerException ex) {
Toast.makeText(this, "Search Term not found", Toast.LENGTH_LONG).show();
ex.getStackTrace();
}
setListAdapter(resultListAdapter);
}
/**
* Returns the items found and writes the items found to an arraylist.
* @return
*/
private ArrayList<SearchResultModel> itemsFound(Cursor cursor, String itemName, String itemDescription, int type) {
// ArrayList<String> list = new ArrayList<String>();
if (cursor.moveToFirst()) {
do {
searchResultList.add(new SearchResultModel(itemName, itemDescription, type, ICON));
} while (cursor.moveToNext());
}
cursor.close();
return searchResultList;
}
}
希望这有帮助
答案 3 :(得分:1)
&#34;我的res / xml / searchable.xml文件(注意:我必须在res文件夹中创建一个&#34; xml&#34;文件夹,因为其中一个文件夹不存在。我不要认为这应该有所作为吗?&#34;
您可以根据需要添加文件夹,只要您遵循xmls&#39;的指导。名称空间
关于搜索,我不相信minSdkVersion和targetSdkVersion是对的。
根据Android's, Remaining Backward Compatible article你应该使用
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />
<application>
...