gpt4 book ai didi

java - 在 Android 中调用可搜索 Activity ?

转载 作者:行者123 更新时间:2023-11-29 09:18:01 25 4
gpt4 key购买 nike

我正在关注 http://developer.android.com/guide/topics/search/search-dialog.html为我的应用创建可搜索的 Activity 。

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",但在您的所有 Activity 引用中使用 samples.xxxxxxxxx.SearchableActivity。尝试将您的应用程序包名称更新为 "samples.xxxxx" 并查看是否可以解决问题。此外,在您的代码中,您将包声明为 package com.xxxx.xx;。因此,下定决心并使所有引用保持一致。

关于java - 在 Android 中调用可搜索 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com