主要实现的效果是和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/

更多相关文章

  1. “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
  2. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  3. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  4. Android蓝牙一篇看懂
  5. Android(安卓)Paging Library 基于RecyclerView的分页加载框架
  6. Android应用开发实例篇(2)-----挂接电震动
  7. Android(安卓)NoSql数据库框架-SnappyDB
  8. 关于ExpandableListView用法的一个简单小例子
  9. Android(安卓)中的 adapter

随机推荐

  1. Android逆向之旅---Android中的sharedUse
  2. Android属性动画使用浅析
  3. android(NDK+JNI)---Android使用JNI实现J
  4. android源码
  5. Android可折叠收缩伸展的Expandable分组R
  6. Android开发学习之基本控件概览
  7. 我和我的Android
  8. Android(安卓)Studio 第六十四期 - Andro
  9. 从Android发展看Meego
  10. Anbox:容器中的_Android