首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android searchable未打开

android searchable未打开
EN

Stack Overflow用户
提问于 2011-01-03 02:36:56
回答 1查看 3.5K关注 0票数 1

您好,我正尝试在我的应用程序中使用可搜索的活动,但是当按下搜索按钮时,什么也没有发生

AndroidManifest.xml

代码语言:javascript
复制
<?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>

xml (res/xml/ Searchable.xml )

代码语言:javascript
复制
<?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>

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

代码语言:javascript
复制
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);
        }
    }
}

蒂娅

ng93

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-03 05:48:12

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

我刚刚写了一个,根据我昨天问的一个问题的答案,Trying to filter a ListView with runQueryOnBackgroundThread but nothing happens - what am I missing?

查看该文档,了解如何与内置搜索支持:Using the Android Search Dialog集成,并查看本文,了解如何在客户类型:Adding Custom Suggestions中提供建议

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

代码语言:javascript
复制
        showResults(query);

这个方法看起来像这样:

代码语言:javascript
复制
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 ();
        }
    });
}

member myDbHelper在我的数据库中查询传入了查询字符串的国家/地区,并将它们显示在列表中。我的activity有一个布局,如下所示:

代码语言:javascript
复制
<?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调用-我需要它,因为我要将国家代码字段转换为一个旗帜图标。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4579837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档