package com.example.search;
import android.content.SearchRecentSuggestionsProvider;
public class SuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "com.example.search.SuggestionProvider" ;
public final static int MODE = DATABASE_MODE_QUERIES;
public SuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
}
* This source code was highlighted with Source Code Highlighter .
public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;
* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? >
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.example.search"
android:versionCode ="1"
android:versionName ="1.0" >
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".Main"
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" />
</ intent-filter >
< meta-data
android:name ="android.app.searchable"
android:resource ="@xml/searchable"
/>
</ activity >
< provider android:name =".SuggestionProvider"
android:authorities ="com.example.search.SuggestionProvider" />
</ application >
< uses-sdk android:minSdkVersion ="5" />
</ manifest >
* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? >
< searchable xmlns:android ="http://schemas.android.com/apk/res/android"
android:label ="@string/app_name"
android:hint ="@string/search_hint"
android:searchSuggestAuthority ="com.example.search.SuggestionProvider"
android:searchSuggestSelection =" ?" >
</ searchable >
* This source code was highlighted with Source Code Highlighter .
package com.example.search;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
public class Main extends ListActivity {
private EditText text;
private Button add;
private RecordsDbHelper mDbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
mDbHelper = new RecordsDbHelper( this );
//
mDbHelper.open();
// Intent
Intent intent = getIntent();
// Intent
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
//
String query = intent.getStringExtra(SearchManager.QUERY);
// SearchRecentSuggestions
SearchRecentSuggestions suggestions = new SearchRecentSuggestions( this ,
SuggestionProvider.AUTHORITY, SuggestionProvider.MODE);
//
suggestions.saveRecentQuery(query, null );
//
showResults(query);
}
add = (Button) findViewById(R.id.add);
text = (EditText) findViewById(R.id.text);
add.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
String data = text.getText().toString();
if (!data.equals( "" )) {
saveTask(data);
text.setText( "" );
}
}
});
}
private void saveTask( String data) {
mDbHelper.createRecord(data);
}
private void showResults( String query) {
//
Cursor cursor = mDbHelper.fetchRecordsByQuery(query);
startManagingCursor(cursor);
String [] from = new String [] { RecordsDbHelper.KEY_DATA };
int [] to = new int [] { R.id.text1 };
SimpleCursorAdapter records = new SimpleCursorAdapter( this ,
R.layout.record, cursor, from , to);
//
setListAdapter(records);
}
// ( res/menu/main_menu.xml)
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true ;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search_record:
//
onSearchRequested();
return true ;
case R.id.clear_recent_suggestions:
//
SearchRecentSuggestions suggestions = new SearchRecentSuggestions( this ,
SuggestionProvider.AUTHORITY, SuggestionProvider.MODE);
suggestions.clearHistory();
return true ;
default :
return super.onOptionsItemSelected(item);
}
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/111493/
All Articles