<? 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"
>
</ searchable >
* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? >
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent" >
< LinearLayout
android:orientation ="horizontal"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:gravity ="top" >
< EditText
android:id ="@+id/text"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:hint ="@string/text"
android:layout_weight ="100.0" />
< Button
android:id ="@+id/add"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="@string/add" />
</ LinearLayout >
< ListView
android:id ="@android:id/list"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content" />
< TextView
android:layout_gravity ="left"
android:id ="@android:id/empty"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:text ="@string/no_records" />
</ LinearLayout >
* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? >
< TextView
android:id ="@+id/text1"
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
/>
* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? >
< resources >
< string name ="app_name" > SearchExample </ string >
< string name ="add" > Add </ string >
< string name ="text" > Enter text </ string >
< string name ="no_records" > There are no records in the table </ string >
< string name ="search_hint" > Search the records </ string >
< string name ="search" > Search </ string >
</ resources >
* 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 >
</ application >
< uses-sdk android:minSdkVersion ="5" />
</ manifest >
* 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.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);
//
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 ;
default :
return super.onOptionsItemSelected(item);
}
}
}
* This source code was highlighted with Source Code Highlighter .
package com.example.search;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class RecordsDbHelper {
public static final String KEY_DATA = "data" ;
public static final String KEY_ROWID = "_id" ;
private static final String TAG = "RecordsDbHelper" ;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE = "CREATE TABLE records(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "data TEXT NOT NULL);" ;
private static final String DATABASE_NAME = "data" ;
private static final String DATABASE_TABLE = "records" ;
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null , DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data" );
db.execSQL( "DROP TABLE IF EXISTS tasks" );
onCreate(db);
}
}
public RecordsDbHelper(Context ctx) {
this .mCtx = ctx;
}
public RecordsDbHelper open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this ;
}
public void close() {
mDbHelper.close();
}
//
public long createRecord( String data) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATA, data);
return mDb.insert(DATABASE_TABLE, null , initialValues);
}
// LIKE
public Cursor fetchRecordsByQuery( String query) {
return mDb.query( true , DATABASE_TABLE, new String [] { KEY_ROWID,
KEY_DATA }, KEY_DATA + " LIKE" + "'%" + query + "%'" , null ,
null , null , null , null );
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/111475/
All Articles