在 Android 中调用可搜索活动?
我正在关注 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"/>
我已添加所有这些,但设备搜索按钮未打开搜索对话框。为什么?
I'm following http://developer.android.com/guide/topics/search/search-dialog.html to create a searchable activity for my app.
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"/>
I have added all these but the device SEARCH button is not opening the search dialog. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对此不确定,但这可能是因为您将应用程序的包声明为
package="com.xxxxxxx.tests"
,但在所有活动引用中您都使用samples。 xxxxxxxxx.SearchableActivity
。尝试将您的应用程序包名称更新为“samples.xxxxx”
,看看是否可以修复该问题。此外,在您的代码中,您将包声明为package com.xxxx.xx;
。因此,请下定决心并使所有参考文献保持一致。I'm not sure about this, but it might be because you declare your application's package as
package="com.xxxxxxx.tests"
, but in all of your activity references you usesamples.xxxxxxxxx.SearchableActivity
. Try updating your app package name to"samples.xxxxx"
and see if that fixes it. Also, in your code you have your package declared aspackage com.xxxx.xx;
. So, make up your mind and make all of the references consistent.