1.配置search bar的相关信息,位置res/xml/searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/search_label" android:hint="@string/search_hint" android:searchSettingsDescription="@string/search_setting_description" android:searchSuggestAuthority="com.android.mms.SuggestionsProvider" android:searchSuggestSelection="pattern" android:searchSuggestIntentAction="android.intent.action.SEARCH" android:includeInGlobalSearch="true" />
说明:label和hint要有,其中label默认没有显示,如果想要显示,添加:
android:searchMode ="showSearchLabelAsBadge" 一般不用。
searchSettingsDescription为搜索设置项中短信息搜索列表项的说明
searchSuggestAuthority为对应的权限声明,其值与manifest.xml中SuggestionsProvider类的android:authorities的值相同。
includeInGlobalSearch为是否包含在全局搜索中,为true时,搜索设置项可以检索到它。
2.manifest.xml搜索结果activity配置
<activity android:name=".ui.SearchActivity"
android:label="@string/search"
android:configChanges="orientation|keyboardHidden" >
<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>
说明:此activity用来显示搜索结果列表界面,继承自ListActivity。
固定配置要有。
3.是所有的应用程序都可以调用搜索键
<meta-data android:name="android.app.default_searchable"
android:value=".ui.SearchActivity" />
其中SearchActivity为执行并显示搜索结果的activity。
4.提供搜索功能的provider
<!-- Search Suggestions Provider -->
<provider android:name="SuggestionsProvider"
android:readPermission="android.permission.READ_SMS"
android:authorities="com.android.mms.SuggestionsProvider" >
<path-permission
android:pathPrefix="/search_suggest_query"
android:readPermission="android.permission.GLOBAL_SEARCH" />
<path-permission
android:pathPrefix="/search_suggest_shortcut"
android:readPermission="android.permission.GLOBAL_SEARCH" />
</provider>
5.搜索键响应函数
在当前的应用程序界面,按下搜索键时,会自动调用onSearchRequested()方法,你可以重写这个方法,做你想要的操作。如果不重写,
会掉用其父类startSearch(...)
如果想在某个activity中屏蔽掉搜索框,重写
@Override
public boolean onSearchRequested() {
return false;
}即可。
或者如果你想进行一些操作
@Override
public boolean onSearchRequested() {
doSomeThing();//some thing you want to do
return super.onSearchRequested();
}
6.响应搜索功能
public class SearchActivity extends ListActivity{
@Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.search_activity);
String searchStringParameter = getIntent().getStringExtra(SearchManager.QUERY);
//做你自己的查询数据操作
doMySearch(searchStringParameter);
...
}
...
}
7.自己写了个搜索结果界面,分享一下,仿照短信息写的
public class NoteSearchableActivity extends ListActivity{
private ListView searchList;
private TextView mTextView;
String searchString;
private AsyncQueryHandler mQueryHandler;
private Uri mUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_activity);
mTextView = (TextView) findViewById(android.R.id.empty);
mTextView.setVisibility(View.GONE);
searchList = getListView();
searchList.setItemsCanFocus(true);
searchList.setFocusable(true);
searchList.setClickable(true);
ContentResolver cr = getContentResolver();
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
searchString = intent.getStringExtra(SearchManager.QUERY).trim();
}
mQueryHandler = new AsyncQueryHandler(cr) {
protected void onQueryComplete(int token, Object cookie, Cursor c) {
if (c == null) {
mTextView.setVisibility(View.VISIBLE);
return;
}
int cursorCount = c.getCount();
setTitle(getString(R.string.search_activity_title,searchString));
if(cursorCount>=1){
mTextView.setVisibility(View.VISIBLE);
}
setListAdapter(new CursorAdapter(NoteSearchableActivity.this,
c, false /* no auto-requery */) {
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView noteTitle = (TextView) view.findViewById(R.id.note_list_title);
TextView noteData = (TextView) view.findViewById(R.id.note_list_time);
noteTitle.setText(cursor.getString(3));
// noteData.setText(getDate(cursor.getLong(2)));
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Intent onClickIntent = new Intent(NoteSearchableActivity.this, EditNote.class);
// onClickIntent.putExtra("thread_id", threadId);
// onClickIntent.putExtra("highlight", searchString);
// onClickIntent.putExtra("select_id", rowid);
startActivity(onClickIntent);
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.note_item, parent, false);
return v;
}
});
// Remember the query if there are actual results
// if (cursorCount > 0) {
// SearchRecentSuggestions recent = ((MmsApp)getApplication()).getRecentSuggestions();
// if (recent != null) {
// recent.saveRecentQuery(
// searchString,
// getString(R.string.search_history,
// cursorCount, searchString));
// }
// }
}
};
// mUri = NoteProvider.CONTENT_URI.buildUpon()
// .appendQueryParameter("pattern", searchString).build();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
mQueryHandler.startQuery(0, null, NoteProvider.CONTENT_URI, null, "content"+" like "+"'%"+searchString+"%'", null, null);
super.onStart();
}
}
8.构建Content Provider
在QuickSearchBox中通过ContentResolver调用内容提供者的query(Uri, String[], String, String[], String)方法进行信息搜索,
应用程序必须实现这个方法并且返回搜索到的Cursor对象。
下面将对ContentProvider的query方法的参数进行说明:
第一个参数(uri):
content://authority/suggestion.path/search_suggest_query /queryStr?limit=50
authority:对应searchable.xml文件中的android:searchSuggestAuthority属性。
suggestion.path:对应searchable.xml文件中的android:searchSuggestPath属性。
search_suggest_query : 固定字符串。
queryStr: 查询字符串。
limit : 查询条数。
第二个参数(projection):一直为null。
第三个参数(selection):对应searchable.xml文件中android:searchSuggestSelection属性,ContentProvider的query方法将会调用SQLite数据
库的Query方法,用这个参数组成SQL语句中where后面的条件,如:where name like ?,”name like ? ”就是selection参数的值,注意,
android:searchSuggestSelection属性中配置的查询条件不能用“AND”或“OR”等符号连接多个条件(不能写成:“name like ? AND age like ?”)。
第四个参数(selectionArgs):如果searchable.xml文件中android:searchSuggestSelection属性值不为空,程序会将查询字符串做为这个数组
的第一个元素,也是唯一的元素,在上面第三个参数中定义了查询条件,这个参数的作用是给查询条件中的问号赋值。
第五个参数:一直为null。
该方法将搜索到的数据以cursor对象的形式返回。
本文介绍如何在Android应用中集成搜索功能,包括配置searchbar、设置搜索活动、响应搜索按键及实现自定义搜索结果界面等内容。
8062

被折叠的 条评论
为什么被折叠?



