主要实现的效果是和google搜索一样,实现联想功能,如用户输入a,列表中则显示以a开头的数据库中的信息。下面是实现的效果图:
- 实现过程主要是添加所有的数据到数据库中。
- 调用搜索控件,可见android利用onSearchRequested()调用内部搜索ui组件。
- 根据用户输入的信息显示联想的所有词的列表。
- 根据用户选择,显示相应的结果。
实现的主要代码:
public class SearchProvider extends ContentProvider { @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; }
@Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { return null; }
@Override public boolean onCreate() { // 添加所有的数据 SearchUtil.getInstance().ensureLoaded(); return true; }
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { String query = null; if (uri.getPathSegments().size() > 1) { query = uri.getLastPathSegment().toLowerCase(); } return getSuggestions(query); }
private Cursor getSuggestions(String query) { String processedQuery = query == null ? "" : query.toLowerCase(); List<SearchUtil.Word> words = SearchUtil.getInstance().getMatches( processedQuery);
MatrixCursor cursor = new MatrixCursor(COLUMNS); long id = 0; for (SearchUtil.Word word : words) { cursor.addRow(columnValuesOfWord(id++, word)); }
return cursor; }
private Object[] columnValuesOfWord(long id, SearchUtil.Word word) { return new Object[] { id, // _id word.word, // text1 word.definition, // text2 word.word, // intent_data (included when clicking on item) }; }
private static final String[] COLUMNS = { "_id", SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_TEXT_2, SearchManager.SUGGEST_COLUMN_INTENT_DATA,// 数据传递到intenter中 };
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; }
相关搜索的配置:
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SearchActivty" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <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 android:name=".ResultActivty" android:label="@string/search_query_results"> </activity> <provider android:name="SearchProvider" android:authorities="search" android:syncable="false" /> </application>
在res的xml文件夹下创建searchable.xml
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/search_label" android:searchSuggestAuthority="search" android:searchSuggestIntentAction="android.intent.action.VIEW" > </searchable>
结果返回:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = this.getIntent(); setContentView(R.layout.main); if (Intent.ACTION_VIEW.equals(intent.getAction())) { SearchUtil.Word theWord = SearchUtil.getInstance().getMatches( intent.getDataString().trim().toLowerCase()).get(0); launchWord(theWord); finish(); } else { Button button = (Button) findViewById(R.id.button); button.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { onSearchRequested(); return false; } }); } } private void launchWord(SearchUtil.Word pavilion) { Intent next = new Intent(); next.setClass(this, ResultActivty.class); Bundle bundle = new Bundle(); bundle.putString("word", pavilion.word); next.putExtras(bundle); next.putExtras(bundle); startActivity(next); }
其中联想的列表还可以实现自定义的列表,需要进一步细化。
源代码:http://easymorse-android.googlecode.com/svn/tags/search/