我遵循http://developer.android.com/guide/topics/search/search-dialog.html为我的应用程序创建一个可搜索的活动。
SearchableActivity.java
package com.xxxx.xx;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import com.xxxx.xx;
public class SearchableActivity extends ListActivity {
protected SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
public List<String> doMySearch(String query) {
List<String> result = new ArrayList<String>();
Cursor c = db.query(
"employee",
new String[] { "_id" }, // The column you want as a result of the Query
"firstName like '%?%' OR lastName like '%?%' OR officePhone like '%?%'", // The where-Clause of the statement with placeholders (?)
new String[] { query, query, query }, // One String for every Placeholder-? in the where-Clause
null, // if you like a order by put it here
null, // group by here
null // having here
);
while (c.moveToNext()) {
result.add(c.getString(0));
}
c.close();
return result;
}
}
/res/xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable> xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxx.xx"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.CALL_PHONE" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<meta-data android:name="android.app.default_searchable"
android:value="com.xxxx.xx.SearchableActivity" />
<activity android:name="com.xxxx.xx.TestList"
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="com.xxxx.xx.SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
<activity android:name="com.xxxx.xx.TestDetails"></activity>
<activity android:name="com.xxxx.xx.TestList">
<meta-data android:name="android.app.default_searchable"
android:value="com.xxxx.xx.SearchableActivity" />
</activity>
<activity android:name="com.xxxx.xx.DirectReports"></activity>
</application>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14"/>
我已经添加了所有这些,但设备搜索按钮没有打开搜索对话框。为什么? 我不确定这一点,但这可能是因为你声明你的应用程序的包为package="com.xxxxxxx.tests"
,但在你所有的活动引用你使用samples.xxxxxxxxx.SearchableActivity
。尝试更新您的应用程序包名称为"samples.xxxxx"
,看看是否修复它。此外,在代码中将包声明为package com.xxxx.xx;
。所以,下定决心,让所有的引用都一致。