在 Android 中调用可搜索活动?

发布于 2024-12-18 10:59:55 字数 3838 浏览 0 评论 0原文

我正在关注 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2024-12-25 10:59:55

我对此不确定,但这可能是因为您将应用程序的包声明为 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 use samples.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 as package com.xxxx.xx;. So, make up your mind and make all of the references consistent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文