android 搜索打不开

发布于 2024-10-10 00:48:53 字数 2583 浏览 4 评论 0原文

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

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

Hi im trying to use a searchable activity in my application but when the search button is pressed nothing happens

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

空名 2024-10-17 00:48:53

您的 Searchable 活动必须执行某些操作 - 并实际显示结果。

我刚刚按照我昨天问的一个问题的答案所建议的写了一个 尝试使用 runQueryOnBackgroundThread 过滤 ListView 但没有任何反应 - 我错过了什么?

查看此文档以了解如何与内置搜索支持集成:使用 Android 搜索对话框 并查看这篇文章,了解如何根据客户类型提供建议: 添加自定义建议

您的课程可搜索< /code> 获取查询字符串后需要做更多的事情。例如,在我的活动中,在获取查询字符串后,我执行以下操作:

        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 调用 - 我需要它,因为我正在将国家/地区代码字段转换为国旗图标。

Your Searchable activity has to do something - and actually display results.

I just wrote one as suggested by an answer to a question I asked yesterday Trying to filter a ListView with runQueryOnBackgroundThread but nothing happens - what am I missing?

Look at this documentation for how to integrate with the built in search support: Using the Android Search Dialog and look at this article on how to offer suggestions as the customer types: Adding Custom Suggestions

Your class Searchable needs to do a bit more after getting the query string. For example in my activity after getting the query string I do this:

        showResults(query);

and that method looks like this:

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 ();
        }
    });
}

The member myDbHelper queries my database for countries that have the passed in query string and displays them in a list. My activity has a layout that looks like this:

<?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>

You can probably do without the setViewBinder call - I need that because I am translating the country code field into a flag icon.

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