我有一个应用程序,我希望在其中添加搜索功能,我正在尝试按照中所述实施developer.android
但是当我单击模拟器中的搜索时,活动没有启动,问题是什么?
-
SearchActivity.java
public class SearchActivity extends ListActivity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
public void onListItemClick(ListView l,
View v, int position, long id) {
// call detail activity for clicked entry
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query =intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String queryStr) {
// get a Cursor, prepare the ListAdapter
// and set it
}
}
-
xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/srchHint"
android:inputType="textCapCharacters">
</searchable>
-
AndroidManifest.xml
(下面的代码不显示其他活动)
<application>
<!-- other actvities -->
<activity android:label="@string/app_name" android:launchMode="singleTop"
android:name=".SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
</activity>
</application>
已经尝试过的事情:
1.在 StackOverflow 上查看类似的问题及其解决方案
2.查看教程格罗克金安卓 and 教育移动,他们没有帮助我的问题。
3.花时间在 chat.stackoverflow 上询问人们
问题/疑问
1.好吧,我有doSearch()
方法,但是如何准确地触发查询以及如何显示数据库的结果(用一个小数据库来显示示例,如果可以的话),如何填充结果。
2.当我在模拟器中单击搜索时,搜索活动不会启动。
3.在设计搜索时,除了developer.android
东西,其他细节很多时候该网站没有透露。
4.如何执行全文搜索,我在 上读到过developer.android
,如何让我的数据库有一个 FTS
P.S:我是刚开始学习android,所以如果有菜鸟错误,请多多包涵,不要跳过步骤。
因此,搜索是决定应用程序实际可用性并提供必要的用户增强功能的关键因素
1.这是处理搜索的最简单方法之一
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query =
intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String queryStr)
{
// get a Cursor/ArrayList, prepare the ListAdapter
// and set it
//helper.getSearchRecords(queryStr);//helper is a SQLiteOpenHelper Object
//set it to adapter or get return result and proceed as you wish
//personally i prefer ArrayAdapter
ListView lstResult=(ListView)findViewById(R.id.lstVwResult);
lstResult.setAdapter(null);
SearchAdapter adapter=new SearchAdapter(getApplicationContext(), R.id.txvPersonId, helper.getSearchRecords(queryStr));
lstResult.setAdapter(adapter);
}
2.SearchActivity未启动的原因是<meta-data>
还需要在活动之外定义,就像这样
<application>
<activity android:label="@string/app_name" android:launchMode="singleTop"
android:name=".SearchActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
</activity>
<!--define metadata outside activity in application tag-->
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
3.要记住的关键事项之一是,为什么要向应用程序添加搜索功能,它是一个联系人应用程序,单击检索结果将显示特定联系人的更多详细信息,但对于餐厅应用程序,它将显示餐厅所在位置的详细信息细节上,作为开发者,开发时应该以用户需求为中心,增强用户体验。
4.因为有Full-Text Search
,这并不总是必要的,它仅取决于您希望如何向数据库查询结果,您将考虑哪些字段以及它们与您的应用程序的主要目的有何相关,
更多参考
-
Sqlite.org(这是关于“如何和什么”的最广泛的文章FTS).
-
博客.andresteingress(FTS 与 android 教程)。
-
O Reilly.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)