我想有历史在我搜索查看,我一直在Google上搜寻四周,唯一明智的(?)教程中,我发现了的这个,但是这只是对于像姜饼,没有API> 14
I want to have history on my SearchView, I've been googling around, and the only sensible(?) tutorial I found was this, but that's just for like Gingerbread, not API>14.
后来我发现这个code:
Then I found this code:
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);
这code只能一半,因为它并不显示从你已经写的结果,它显示了所有项目。
And that code only works half, since it doesn't show results from what you've already written, it shows all the items.
我只是希望它看起来是这样的:
I just want it to look like this:
这是我目前的$ C $下加入搜索查看:
This is my current code for adding the SearchView:
RES /菜单/ menu.xml文件:
res/menu/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:
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);
}
可能有人请你只给我的东西下手,说实话,我不知道从哪里开始。因此,任何帮助将是非常美联社preciated。
Could someone please just give me something to start with, to be honest I have no idea where to start. So any help would be really appreciated.
这有关如何实现历史搜索查看网页的会谈。
This page talks about how you can implement history for SearchView.
的http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html
首先,你必须创建一个内容提供商:
First, you have to create a content provider:
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);
}
}
然后声明内容提供者在这样的应用程序清单:
Then declare the content provider in your application manifest like this:
<application>
<provider android:name=".MySuggestionProvider"
android:authorities="com.example.MySuggestionProvider" />
...
</application>
那么,内容提供者添加到您搜索的配置是这样的:
Then add the content provider to your searchable configurations like this:
<?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方法为您的活动:
You can call saveRecentQuery() to save queries at any time. Here's how you can do it in the onCreate method for your activity:
@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()是这样的:
To clear search history you simply need to call SearchRecentSuggestions's method clearHistory() like this:
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
HelloSuggestionProvider.AUTHORITY, HelloSuggestionProvider.MODE);
suggestions.clearHistory();