android可搜索无法打开

时间:2011-01-02 18:36:56

标签: android search searchable

您好我试图在我的应用程序中使用可搜索的活动但是当按下搜索按钮时没有任何反应

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.test" android:versionCode="1" android:versionName="1.0.0" android:configChanges="keyboardHidden|orientation">
        <uses-sdk android:minSdkVersion="7"/>
        <application android:icon="@drawable/icon" android:label="Test">
            <activity android:name=".Test" android:label="Test" android:debuggable="true" android:theme="@android:style/Theme.NoTitleBar" android:launchMode="singleTask">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
            <activity android:name=".Searchable">
                <intent-filter>
                    <action android:name="android.intent.action.SEARCH" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
                <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
            </activity>
            <meta-data android:name="android.app.default_searchable" android:value=".Searchable"/>
        </application>
    </manifest>

Searchable.xml(res / xml / searchable.xml)

<?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="Search" android:hint="Perform Search">
    </searchable>

Searchable.java(src / com / test / test / Searchable.java)

package com.test.test;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;

public class Searchable extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent(getIntent());
    }
    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }
    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
        }
    }
}

TIA,

ng93

1 个答案:

答案 0 :(得分:3)

您的Searchable活动必须做某事 - 并实际显示结果。

我刚刚写了一篇关于我昨天问过Trying to filter a ListView with runQueryOnBackgroundThread but nothing happens - what am I missing?

的问题的回答

请查看此文档,了解如何与内置搜索支持集成Using the Android Search Dialog,并查看有关如何根据客户类型提供建议的文章:Adding Custom Suggestions

获取查询字符串后,您的班级Searchable需要做更多的工作。例如,在获取查询字符串后的我的活动中,我这样做:

        showResults(query);

并且该方法如下所示:

private void showResults(String query)  {
    //  Load the list of countries based on the query
    Cursor countryCursor = myDbHelper.getCountryList (query);
    startManagingCursor (countryCursor);

    //  Hook up the query results to the list view
    String[] from = new String[]  {
        WorldInfoDatabaseAdapter.KEY_COUNTRYCODE, WorldInfoDatabaseAdapter.KEY_COUNTRYNAME
    };
    int[] to = new int[]  {
        R.id.countryflag, R.id.countryname
    };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter (this,
            R.layout.country_list_row, countryCursor, from, to);
    adapter.setViewBinder (new FlagViewBinder ());

    myCountryList.setAdapter (adapter);
    myCountryList.setOnItemClickListener (new OnItemClickListener () {

        @Override
        public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
            String countryName = myDbHelper.getCountryByID (id);
            if (countryName == null)  {
                new AlertDialog.Builder (SelectCountryActivity.this).setMessage (
                        "Internal error: Cannot find the country with id'" + id + "'.").show ();

                return;
            }

            //  Package up the country name to return
            Intent newCountryIntent = new Intent (myCountryList.getContext (), WorldInfoActivity.class);
            newCountryIntent.putExtra (WorldInfoActivity.KEY_SELECTED_COUNTRY, countryName);
            startActivity (newCountryIntent);

            startActivity (newCountryIntent);
            finish ();
        }
    });
}

成员myDbHelper在我的数据库中查询具有传入查询字符串的国家/地区并将其显示在列表中。我的活动有一个如下所示的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    style="?pageBackground">
    <TextView
        android:id="@+id/selectdlgtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/select_country_title"
        style="?dlgTitle" />
    <ListView
        android:id="@+id/countrylist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:padding="2px"/>
    </LinearLayout>

你可能没有setViewBinder电话 - 我需要这个,因为我正在将国家代码字段翻译成一个标志图标。