我想在我的SearchView上有历史记录,我一直在谷歌搜索,我找到的唯一明智的(?)教程是this,但这只是姜饼,而不是API> 14。
然后我找到了这段代码:
String[] columnNames = {"_id","text"};
MatrixCursor cursor = new MatrixCursor(columnNames);
String[] array = {"Snääälla", "bla bla bla", "Jävla piss"}; //if strings are in resources
String[] temp = new String[2];
int id = 0;
for(String item : array){
temp[0] = Integer.toString(id++);
temp[1] = item;
cursor.addRow(temp);
}
String[] from = {"text"};
int[] to = {android.R.id.text1};
CursorAdapter ad = new SimpleCursorAdapter(this.getActivity(), android.R.layout.simple_list_item_1, cursor, from, to);
mSearchView.setSuggestionsAdapter(ad);
而且该代码只能工作一半,因为它没有显示您已经编写的内容的结果,它显示了所有项目。
我只是希望它看起来像这样:
这是我目前添加SearchView的代码:
RES /菜单/ menu.xml文件:
<item android:id="@+id/fragment_searchmenuitem"
android:icon="@drawable/ic_search_white"
android:title="@string/menu_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
MainActivity.java:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if(!mDrawerLayout.isDrawerOpen(mDrawerList)) {
inflater.inflate(R.menu.fragment_search, menu);
mMenuItem = menu.findItem(R.id.fragment_searchmenuitem);
mSearchView = (SearchView) mMenuItem.getActionView();
mMenuItem.expandActionView();
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
mMenuItem.collapseActionView();
searchSupport.SearchForLyrics(s);
actionBar.setSubtitle("Searcing for: " + s);
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
super.onCreateOptionsMenu(menu, inflater);
}
有人可以请给我一些开始的东西,说实话我不知道从哪里开始。所以任何帮助都会非常感激。
答案 0 :(得分:6)
此页面介绍了如何为SearchView实现历史记录。
http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html
首先,您必须创建内容提供商:
public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "com.example.MySuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES;
public MySuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
}
然后在应用程序清单中声明内容提供程序,如下所示:
<application>
<provider android:name=".MySuggestionProvider"
android:authorities="com.example.MySuggestionProvider" />
...
</application>
然后将内容提供商添加到您的可搜索配置中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint"
android:searchSuggestAuthority="com.example.MySuggestionProvider"
android:searchSuggestSelection=" ?" >
</searchable>
您可以随时调用saveRecentQuery()来保存查询。以下是如何在您的活动的onCreate方法中执行此操作:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);
}
}
要清除搜索记录,您只需要调用 SearchRecentSuggestions 的方法 clearHistory(),如下所示:
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
HelloSuggestionProvider.AUTHORITY, HelloSuggestionProvider.MODE);
suggestions.clearHistory();
答案 1 :(得分:0)
我在ActionBar上为SeaarchView使用Fragment,所以我有自己的侦听器,例如“ setOnSuggestionListener”,“ setOnQueryTextListener”。当我编写searchview.setSearchableInfo()时,我的适配器停止工作。因此,我查看了“ setSearchableInfo”函数,并提取了一些代码以从核心代码中自己获取历史搜索数据。
class MySearchableInfoClass internal constructor(
private val mContext: Context,
private val mSearchable: SearchableInfo
) {
private val QUERY_LIMIT = 5
private fun getSearchManagerSuggestions(
searchable: SearchableInfo?,
query: String,
limit: Int
): Cursor? {
if (searchable == null) {
return null
}
val authority = searchable.suggestAuthority ?: return null
val uriBuilder = Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(authority)
.query("") // TODO: Remove, workaround for a bug in Uri.writeToParcel()
.fragment("") // TODO: Remove, workaround for a bug in Uri.writeToParcel()
// if content path provided, insert it now
val contentPath = searchable.suggestPath
if (contentPath != null) {
uriBuilder.appendEncodedPath(contentPath)
}
// append standard suggestion query path
uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY)
// get the query selection, may be null
val selection = searchable.suggestSelection
// inject query, either as selection args or inline
var selArgs: Array<String>? = null
if (selection != null) { // use selection if provided
selArgs = arrayOf(query)
} else { // no selection, use REST pattern
uriBuilder.appendPath(query)
}
if (limit > 0) {
uriBuilder.appendQueryParameter("limit", limit.toString())
}
val uri = uriBuilder.build()
// finally, make the query
return mContext.contentResolver.query(uri, null, selection, selArgs, null)
}
fun getSearchHistoryCursor(constraint: CharSequence?): Cursor? {
val query = constraint?.toString() ?: ""
var cursor: Cursor? = null
try {
cursor = getSearchManagerSuggestions(mSearchable, query, QUERY_LIMIT)
// trigger fill window so the spinner stays up until the results are copied over and
// closer to being ready
if (cursor != null) {
cursor.count
return cursor
}
} catch (e: RuntimeException) {
}
// If cursor is null or an exception was thrown, stop the spinner and return null.
// changeCursor doesn't get called if cursor is null
return null
}
}
getSearchHistoryCursor返回一个游标,以便您可以使用getString或其他任何内容,并最终搜索历史记录。
示例:
cursor.getString(cursor.getColumnIndex("suggest_text_1")))