Android的搜索不开放 [英] android searchable not opening
问题描述
即时喜试图用一个可搜索的活动在我的应用程序,但是当搜索按钮pressed什么也不会发生。
Hi im trying to use a searchable activity in my application but when the search button is pressed nothing happens
AndroidManifest.xml中
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)
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 /测试/测试/ Searchable.java)
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
推荐答案
您检索
的活动有做一些事情 - 和实际显示效果
Your Searchable
activity has to do something - and actually display results.
我只写了一个作为一个问题,我昨天问<一个建议的答复href="http://stackoverflow.com/questions/4571401/trying-to-filter-a-listview-with-runqueryonbackgroundthread-but-nothing-happens/4571791#4571791">Trying过滤一个ListView与runQueryOnBackgroundThread但没有任何反应? - 我缺少什么
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?
看看这个文件,了解怎样与内置的搜索支持集成:的使用Android的搜索对话框并看一下这篇文章对如何提供建议,客户类型:的添加自定义建议
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);
和该方法是这样的:
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查询我的数据库已经传入的查询字符串国家和列表的形式显示。我的活动都有一个布局,看起来是这样的:
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>
您也许可以做无 setViewBinder
电话 - 我需要因为我翻译这个国家code字段成一个标志图标
You can probably do without the setViewBinder
call - I need that because I am translating the country code field into a flag icon.
这篇关于Android的搜索不开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!