ããã¯ãAndroidæ€çŽ¢ãã€ã¢ãã°ã®äœ¿çšã«é¢ããæåŸã®èšäºã§ãïŒåã®èšäºã¯ãã¡ããšãã¡ã ïŒã ãã®äžã§ãåçæ€çŽ¢ãã³ãããã€ã¢ãã°ã«è¿œå ããæ¹æ³ãããã³ã¢ããªã±ãŒã·ã§ã³ã®æ€çŽ¢ãã·ã¹ãã ã®ã¯ã€ãã¯æ€çŽ¢ããã¯ã¹ïŒQSBïŒã«çµ±åããæ¹æ³ã説æããŸãã QSBã®å©ç¹ã¯ããã®å©ããåããŠãOSã®ã»ãŒã©ãããã§ãæ å ±ãååŸã§ããããšã§ãã
çè«
æ€çŽ¢ãã³ãã¯ãæ€çŽ¢äžã®ã¢ããªã±ãŒã·ã§ã³ã®ããŒã¿ã䜿çšããŠäœæãããŸãã ãŠãŒã¶ãŒããã®ãã¡ã®1ã€ãéžæãããšãæ€çŽ¢ãããŒãžã£ãŒã¯ãæ€çŽ¢ãæ åœããã¢ã¯ãã£ããã£ã«ã€ã³ãã³ããéä¿¡ããŸãã éåžžããŠãŒã¶ãŒããã€ã¢ãã°ã®æ€çŽ¢ã¢ã€ã³ã³ãã¯ãªãã¯ãããšãæ€çŽ¢ã¿ã€ãã®ã€ã³ãã³ããéä¿¡ãããŸããããã®å Žåã¯ããã³ãããéžæãããšãã«ãå¥ã®ã¿ã€ãã®ã€ã³ãã³ããå®çŸ©ããŠãã€ã³ã¿ãŒã»ããããŠé©åãªã¢ã¯ã·ã§ã³ïŒæ°ãããã€ã¢ãã°ã®äœæãã¢ã¯ãã£ããã£ã®åŒã³åºããªã©ïŒãå®è¡ã§ããŸãæ å ±ãªã©ã衚瀺ãã
æ€çŽ¢ãªã¯ãšã¹ãã®ããŒã¿ã¯ä»¥åãšåæ§ã«Intentãä»ããŠè»¢éãããŸãããããã§ã¯URIã䜿çšããŠã³ã³ãã³ããããã€ããŒãä»ãããªã¯ãšã¹ãã®ã¿ã€ãã決å®ããŸãã
ç¹°ãè¿ããŸããããã€ã¢ãã°ãã¬ã³ããªã³ã°ããããã«ã¢ã¯ã·ã§ã³ãå®è¡ããå¿ èŠã¯ãããŸãããããã¯Search Managerã«ãã£ãŠå®è¡ãããŸããå¿ èŠãªã®ã¯ãæ§æxmlãã¡ã€ã«ãéä¿¡ããããšã ãã§ãã
ãã®ãããSearch Managerãã¢ã¯ãã£ããã£ãæ€çŽ¢ã®è²¬ä»»ãšå®çŸ©ããæ€çŽ¢ã®ãã³ããæäŸãããšã次ã®äžé£ã®ã¢ã¯ã·ã§ã³ãçºçããŸãã
- Search Managerã¯ãæ€çŽ¢ã¯ãšãªã®ããã¹ããåä¿¡ãããšããã³ããæäŸããã³ã³ãã³ããããã€ããŒã«èŠæ±ãéä¿¡ããŸãã
- ã³ã³ãã³ããããã€ããŒã¯ãæ€çŽ¢ã¯ãšãªã®ããã¹ãã«äžèŽãããã³ããæãã«ãŒãœã«ãè¿ããŸãã
- Search Managerã¯ã«ãŒãœã«ã䜿çšããŠããŒã«ãããã衚瀺ããŸã
ããã³ããã®ãªã¹ãã衚瀺ãããåŸã次ã®ããšãçºçããå ŽåããããŸãã
- ãŠãŒã¶ãŒããªã¯ãšã¹ãã®ããã¹ããå€æŽãããšãäžèšã®ãã¹ãŠã®æé ãç¹°ãè¿ãããŸãã
- ãŠãŒã¶ãŒãæ€çŽ¢ãéå§ãããšãããã³ããã¯ç¡èŠãããæ€çŽ¢ã¿ã€ãã®ã€ã³ãã³ããã¢ã¯ãã£ããã£ã«éä¿¡ãããŸãã
- ãŠãŒã¶ãŒããã³ããéžæãããšãç°ãªãã¿ã€ãã®ã€ã³ãã³ããã¢ã¯ãã£ããã£ã«é ä¿¡ããïŒã¿ã€ãã¯èšå®ãã¡ã€ã«ã§å®çŸ©ãããŸãïŒãURIãããŒã¿ãšããŠè»¢éããŸãã URIã¯ãéžæããããã³ããã«å¯Ÿå¿ããããŒãã«å ã®ã¬ã³ãŒããèŠã€ããããã«äœ¿çšãããŸãã
ãã®ãããåçãªããŒã«ããããè¿œå ãããããã«ã¢ããªã±ãŒã·ã§ã³ïŒ ããŒã1ã§èª¬æãããã®ïŒãå€æŽããã¡ã«ããºã ãåäœãããããã«ãããŒã«ããããéžæãããšãã«ãæ°ããã¢ã¯ãã£ããã£ãåŒã³åºããŸããããã¯ãèŠæ±ã«å¿ããŠæ å ±ã衚瀺ããŸãã å®è£ ã«ã¯æ¬¡ã®ãã®ãå¿ èŠã§ãã
- ãã€ã¢ãã°æ§æãã¡ã€ã«ãå€æŽããŠãã³ã³ãã³ããããã€ããŒãšãã³ãã«äœ¿çšãããã€ã³ãã³ãã®ã¿ã€ãã«é¢ããæ å ±ãè¿œå ããŸãã
- Search Managerãããã³ããã«å¿ èŠãªåãæäŸããããŒãã«ãSQLiteããŒã¿ããŒã¹ã«äœæããŸã
- ãã³ãããŒãã«ã«ã¢ã¯ã»ã¹ã§ããæ°ããã³ã³ãã³ããããã€ããŒãäœæãããããã§ã¹ãã§å®çŸ©ãã
- ããã³ãããéžæãããšãã«æ å ±ã衚瀺ããã¢ã¯ãã£ããã£ãè¿œå ããŸã
æ§æãã¡ã€ã«ãå€æŽãã
é³å£°æ€çŽ¢ã䜿çšããå Žåãªã©ããã€ã¢ãã°ã衚瀺ããŠå€æŽããã«ã¯ãæ§æãã¡ã€ã«ïŒres / xml / searchable.xmlïŒãå¿ èŠã§ããããšãæãåºããŸãã åçãªãã³ãã䜿çšããã«ã¯ãandroidïŒsearchSuggestAuthorityãã©ã¡ãŒã¿ãŒããã¡ã€ã«ã«è¿œå ããå¿ èŠããããŸãã ã³ã³ãã³ããããã€ããŒã®èªèšŒæååãšäžèŽããŸãã ããã«ããã©ã¡ãŒã¿androidïŒsearchMode = "queryRewriteFromText"ãè¿œå ããŸãããã®å€ã¯ãããšãã°ãã©ãã¯ããŒã«ã䜿çšããŠããã³ãããããã²ãŒããããšããã€ã¢ãã°å ã®æ€çŽ¢æååãäžæžããããããšã瀺ããŸãã ãŸããéžææŒç®åãããŒã«ããããéžæããããšãã«éä¿¡ãããã€ã³ãã³ãã®ã¿ã€ããããã³ã³ã³ãã³ããããã€ããŒãèŠæ±ããããã«å¿ èŠãªãã€ã¢ãã°å ã®å ¥åæåã®æå°æ°ãæå®ãããã©ã¡ãŒã¿ãŒãè¿œå ããŸãã
ãã¡ã€ã«res / xml / searchable.xml
<?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:searchSettingsDescription="@string/settings_description" android:searchMode="queryRewriteFromText" android:includeInGlobalSearch="true" android:searchSuggestAuthority="com.example.search.SuggestionProvider" android:searchSuggestIntentAction="android.intent.action.VIEW" android:searchSuggestIntentData="content://com.example.search.SuggestionProvider/records" android:searchSuggestThreshold="1" android:searchSuggestSelection=" ?"> </searchable>
ã³ã³ãã³ããããã€ããŒãäœæãã
å®éãåœç€Ÿã®ã³ã³ãã³ããããã€ããŒã¯ä»ã®ãããã€ããŒãšéãã¯ãããŸããã ãã ããããã³ããã®è¡šã®åè¡ã«ã€ããŠãæ€çŽ¢ãããŒãžã£ãŒã§å¿ èŠãªåãéžæãããŠããããšã確èªããå¿ èŠããããŸãã ã³ã³ãã³ããããã€ããŒã®queryïŒïŒã¡ãœããã䜿çšããŠãããã³ããã®ããŒã¿ãç §äŒããŸãã ããã«ããŠãŒã¶ãŒããã€ã¢ãã°ã§æ°ããæåãå ¥åãããã³ã«åŒã³åºãããŸãã ãããã£ãŠãqueryïŒïŒã¡ãœããã¯ãã¯ãšãªã«äžèŽããããŒãã«å ã®ã¬ã³ãŒãã«ã«ãŒãœã«ãè¿ãå¿ èŠããããSearch Managerã¯ããã³ããã衚瀺ã§ããããã«ãªããŸãã ã³ãŒãã³ã¡ã³ãã®ã¡ãœããã®èª¬æãåç §ããŠãã ããã
èŠæ±ããã¹ãèªäœã¯URIã«è¿œå ããããããåä¿¡ã«åé¡ã¯ãããŸãã;æšæºã®getLastPathSegmentïŒïŒã¡ãœããã䜿çšããã ãã§ãã
ãã³ãè¡šã®äœæ
Search Managerã¯ãã¬ã³ãŒããæãã«ãŒãœã«ãåãåããšãåã¬ã³ãŒãã«ç¹å®ã®åã»ãããå¿ èŠã§ãã 2ã€ã¯å¿ é ã§ãã_IDã¯åããŒã«ãããã®äžæã®èå¥åã§ãSUGGEST_COLUMN_TEXT_1ã¯ããŒã«ãããããã¹ãã§ãã
ããšãã°ãSUGGEST_COLUMN_ICON_1ã䜿çšããŠãããŒã«ãããã®å·ŠåŽã«è¡šç€ºãããã¢ã€ã³ã³ãã¬ã³ãŒãããšã«å®çŸ©ã§ãããªãã·ã§ã³ã®åãå€æ°ãããŸãïŒé£çµ¡å ã®æ€çŽ¢ãªã©ã«éåžžã«äŸ¿å©ã§ãïŒã
ã€ã³ãã³ãã®ããŒã¿åå®çŸ©
URIãä»ããŠèŠæ±ã«å¿ããŠããŒã¿ãéä¿¡ãããããã©ã®ããŒã«ããããéžæãããããå€æããã¡ã«ããºã ãå¿ èŠã§ãã 2ã€ã®æ¹æ³ããããŸãã 1ã€ç®ã¯ãã¬ã³ãŒãããšã«äžæã®ããŒã¿ãããåå¥ã®åSUGGEST_COLUMN_INTENT_DATAãå®çŸ©ããããšã§ãããã®åŸãgetDataïŒïŒãŸãã¯getDataStringïŒïŒã䜿çšããŠIntentããããŒã¿ãååŸã§ããŸãã 2çªç®ã®ãªãã·ã§ã³ã¯ãæ§æãã¡ã€ã«ïŒres / xml / searchable.xmlïŒå ã®ãã¹ãŠã®ã€ã³ãã³ãã®ããŒã¿åã決å®ããSUGGEST_COLUMN_INTENT_DATA_IDåã䜿çšããŠåã€ã³ãã³ãã®äžæã®ããŒã¿ãURIã«è¿œå ããããšã§ãã
2çªç®ã®ãªãã·ã§ã³ã䜿çšããŸããSUGGEST_COLUMN_INTENT_DATA_IDããããŒãã«ã®rowIdãžã®ãããã³ã°ãç°¡åã«äœæã§ãããããããŒãã«ã«åå¥ã®åãäœæããŸããã§ããã SQLiteãžã®ã¹ããŒãã®é¢å¿ã®ããã«ã FTS3ãæ€çŽ¢ã«äœ¿çšãããŸãããã€ãŸããPRIMARY KEYãNULL / NOT NULLãªã©ã®åïŒå¶çŽïŒã«å¶éã課ãããšãã§ããªãä»®æ³ããŒãã«ãäœæããå¿ èŠããããŸããã ãã ããä»®æ³ããŒãã«ã«ã¯äžæã®è¡èå¥åããããããã«ãããã³ã°ãèšå®ããŸãã ã€ãŸããIntentã®ããŒã¿ã¯æ¬¡ã®åœ¢åŒã«ãªããŸããã/ãããã³ããŒãã«ã®è¡ã®rowIdãURIã«è¿œå ãããŸãã
æ å ±ã衚瀺ããã¢ã¯ãã£ããã£ãäœæãã
ã€ã³ã¿ãŒãã§ã€ã¹ã¯res / layout / record_activity.xmlã«ãããŸãã ãã®ã¢ã¯ãã£ããã£ã¯ãIntentããããŒã¿ãååŸããã³ã³ãã³ããããã€ããŒãä»ããŠã«ãŒãœã«ãèŠæ±ããããã¹ããã£ãŒã«ãã«ã¬ã³ãŒãã衚瀺ããã ãã§ãã
ãã¡ã€ã«res / layout / record_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp"> <TextView android:id="@+id/record_header" android:textSize="25dp" android:textColor="?android:textColorPrimary" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
ããã§ããããã§ã¹ãã«ã³ã³ãã³ããããã€ããŒãšæ°ããã¢ã¯ãã£ããã£ã«é¢ããæ å ±ãå ¥åããŸãããŸãã2ã€ã®ã¢ã¯ãã£ããã£ããããããããã©ã«ãã§æ€çŽ¢ãæ åœããã¢ã¯ãã£ããã£ã瀺ããŸãã
AndroidManifest.xmlãã¡ã€ã«
<?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> <activity android:name=".RecordActivity" android:theme="@android:style/Theme.NoTitleBar" /> <provider android:name=".SuggestionProvider" android:authorities="com.example.search.SuggestionProvider" /> <meta-data android:name="android.app.default_searchable" android:value=".Main" /> </application> <uses-sdk android:minSdkVersion="5" /> </manifest>
æ€çŽ¢ãæ åœããã¢ã¯ãã£ããã£ã§ã€ã³ãã³ããã€ã³ã¿ãŒã»ãããã
äžèšã®ãã¹ãŠã®ã¹ãããã®åŸãæ€çŽ¢ãæ åœããã¡ã€ã³ã¢ã¯ãã£ããã£ã§ã€ã³ãã³ããåŠçããå¿ èŠããããŸãã ããŒã«ãããã®ã€ã³ãã³ãã¿ã€ãããã¥ãŒãšããŠå®çŸ©ããããããã§ãã¯ãè¿œå ããã ãã§ãã æ¡ä»¶ãæºããããå ŽåãRecordActivityã¯Intentã䜿çšããŠèµ·åããããã®ããŒã¿ã«ã¯ãURI + "/" +ããŒãã«ã®ããŒã«ããããæžãââ蟌ãŸããŸãã
ã¯ã€ãã¯æ€çŽ¢ããã¯ã¹ã®çµ±å
ã«ã¹ã¿ã ææ¡ã䜿çšããããã«ã¢ããªã±ãŒã·ã§ã³ãå€æŽããåŸãã·ã¹ãã æ€çŽ¢ã«è¿œå ã§ããŸãã ãããè¡ãã«ã¯ãsearchable.xmlãã¡ã€ã«ã«2ã€ã®ãã©ã¡ãŒã¿ãŒãè¿œå ããŸãã
- androidïŒincludeInGlobalSearch = "true"-QSBãã¢ããªã±ãŒã·ã§ã³ãæ€çŽ¢ã§ããããšã瀺ããŸãã
- androidïŒsearchSettingsDescription = "@ string / settings_description"-ã¢ããªã±ãŒã·ã§ã³ã®èª¬æã瀺ããŸããããã¯ã¯ã€ãã¯æ€çŽ¢ããã¯ã¹ã®èšå®æã«è¡šç€ºãããŸãã ãããã®èšå®ã¯ãèšå®->æ€çŽ¢ã«ãããŸãã
ãããã®ãªãã·ã§ã³ã¯Android 1.6ããå©çšã§ããŸããã€ãŸãã以äžã®ããŒãžã§ã³ã§ã¯ãQSBçšã«ã¢ããªã±ãŒã·ã§ã³ãæ§æããããšã¯ã§ããŸããã
ãœãŒã¹ã³ãŒã
å¿ èŠãªãã¹ãŠã®ã¯ã©ã¹ã®å®å šãªãœãŒã¹ã³ãŒãã瀺ããŸãã Main.java-èŠæ±ãèŠã€ããŠã³ã³ãã³ããããã€ããŒã«éä¿¡ããã¡ã€ã³ã®ã¢ã¯ãã£ããã£ã§ããRecordActivity.java-ç¹å®ã®ã¬ã³ãŒãã®ããŒã¿ãå«ãã€ã³ãã³ããåãåããã¬ã³ãŒããžã®ãªã³ã¯ãåãåããæ å ±ã衚瀺ããŸãã SuggestionProvider.javaã¯ãSearch Managerããããã³ããã®ããŒãã«ãžã®ãªã¯ãšã¹ããåŠçããã³ã³ãã³ããããã€ããŒã§ãã RecordsDbHelper.java-ããŒãã«ã®äœæãããŒãã«ãžã®å ¥åãå¿ èŠãªè¡šç€ºã®ç¢ºç«ãããã³ã¬ã³ãŒãã®ããããã³ã°ãèªäœãè¡ããŸãã
Main.javaãã¡ã€ã«
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; import android.widget.Toast; 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); Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { // String query = intent.getStringExtra(SearchManager.QUERY); // showResults(query); } else if (Intent.ACTION_VIEW.equals(intent.getAction())){ // Intent RecordActivity Intent recordIntent = new Intent(this, RecordActivity.class); recordIntent.setData(intent.getData()); startActivity(recordIntent); finish(); } 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 = managedQuery(SuggestionProvider.CONTENT_URI, null, null, new String[] {query}, null); if (cursor == null) { Toast.makeText(this, "There are no results", Toast.LENGTH_SHORT).show(); } else { // 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); getListView().setAdapter(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); } } }
ãã¡ã€ã«RecordActivity.java
package com.example.search; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.TextView; public class RecordActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.record_activity); // URI Intent - Uri uri = getIntent().getData(); Cursor cursor = managedQuery(uri, null, null, null, null); if (cursor == null) { finish(); } else { // cursor.moveToFirst(); TextView record = (TextView) findViewById(R.id.record_header); int rIndex = cursor.getColumnIndexOrThrow(RecordsDbHelper.KEY_DATA); record.setText(cursor.getString(rIndex)); } } // @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.search_record: onSearchRequested(); return true; default: return false; } } }
ãã¡ã€ã«SuggestionProvider.java
package com.example.search; import android.app.SearchManager; import android.content.ContentProvider; import android.content.ContentResolver; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.net.Uri; import android.provider.BaseColumns; public class SuggestionProvider extends ContentProvider{ private RecordsDbHelper mDbHelper; public static String AUTHORITY = "com.example.search.SuggestionProvider"; public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/records"); //MIME getType() public static final String RECORDS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.example.search"; public static final String RECORD_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.example.search"; // URI private static final int SEARCH_RECORDS = 0; private static final int GET_RECORD = 1; private static final int SEARCH_SUGGEST = 2; private static final UriMatcher sURIMatcher = makeUriMatcher(); @Override public boolean onCreate() { mDbHelper = new RecordsDbHelper(getContext()); return true; } /** * Search Manager'a. * , URI. * , selectionArgs . * . */ @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // UriMatcher, . switch (sURIMatcher.match(uri)) { case SEARCH_SUGGEST: if (selectionArgs == null) { throw new IllegalArgumentException( "selectionArgs must be provided for the Uri: " + uri); } return getSuggestions(selectionArgs[0]); case SEARCH_RECORDS: if (selectionArgs == null) { throw new IllegalArgumentException( "selectionArgs must be provided for the Uri: " + uri); } return search(selectionArgs[0]); case GET_RECORD: return getRecord(uri); default: throw new IllegalArgumentException("Unknown Uri: " + uri); } } private Cursor getSuggestions(String query) { query = query.toLowerCase(); String[] columns = new String[] { BaseColumns._ID, RecordsDbHelper.KEY_DATA, SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID}; return mDbHelper.getRecordMatches(query, columns); } private Cursor search(String query) { query = query.toLowerCase(); String[] columns = new String[] { BaseColumns._ID, RecordsDbHelper.KEY_DATA}; return mDbHelper.getRecordMatches(query, columns); } private Cursor getRecord(Uri uri) { String rowId = uri.getLastPathSegment(); String[] columns = new String[] { RecordsDbHelper.KEY_DATA}; return mDbHelper.getRecord(rowId, columns); } /** * * URI */ private static UriMatcher makeUriMatcher() { UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); // matcher.addURI(AUTHORITY, "records", SEARCH_RECORDS); matcher.addURI(AUTHORITY, "records/#", GET_RECORD); // matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST); matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST); return matcher; } // ( ContentProvider) @Override public String getType(Uri uri) { switch (sURIMatcher.match(uri)) { case SEARCH_RECORDS: return RECORDS_MIME_TYPE; case SEARCH_SUGGEST: return SearchManager.SUGGEST_MIME_TYPE; case GET_RECORD: return RECORD_MIME_TYPE; default: throw new IllegalArgumentException("Unknown URL " + uri); } } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { throw new UnsupportedOperationException(); } @Override public Uri insert(Uri uri, ContentValues values) { throw new UnsupportedOperationException(); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { throw new UnsupportedOperationException(); } }
ãã¡ã€ã«RecordsDbHelper.java
package com.example.search; import java.util.HashMap; import android.app.SearchManager; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteQueryBuilder; import android.provider.BaseColumns; import android.util.Log; public class RecordsDbHelper { // - public static final String KEY_DATA = SearchManager.SUGGEST_COLUMN_TEXT_1; private static final String TAG = "RecordsDbHelper"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; private static final String DATABASE_NAME = "datas"; private static final String DATABASE_TABLE = "records"; private static final int DATABASE_VERSION = 2; // private static final String DATABASE_CREATE = "CREATE VIRTUAL TABLE " + DATABASE_TABLE + " USING fts3 (" + KEY_DATA + ");"; private static final HashMap<String,String> mColumnMap = buildColumnMap(); /** * , rowId * @param rowId id * @param columns ; null, * @return , , null - */ public Cursor getRecord(String rowId, String[] columns) { String selection = "rowid = ?"; String[] selectionArgs = new String[] {rowId}; return query(selection, selectionArgs, columns); } /** * , , * @param query * @param columns ; null, * @return , , , null - */ public Cursor getRecordMatches(String query, String[] columns) { String selection = KEY_DATA + " MATCH ?"; String[] selectionArgs = new String[] {query+"*"}; return query(selection, selectionArgs, columns); } /** * . * SQLiteQueryBuilder. * , SUGGEST_COLUMN_INTENT_DATA_ID * URI. */ private static HashMap<String,String> buildColumnMap() { HashMap<String,String> map = new HashMap<String,String>(); map.put(KEY_DATA, KEY_DATA); map.put(BaseColumns._ID, "rowid AS " + BaseColumns._ID); map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID); return map; } /** * * @param selection * @param selectionArgs , "?" * @param columns * @return , , */ private Cursor query(String selection, String[] selectionArgs, String[] columns) { /* SQLiteBuilder * , - * . */ SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); builder.setTables(DATABASE_TABLE); builder.setProjectionMap(mColumnMap); Cursor cursor = builder.query(mDbHelper.getReadableDatabase(), columns, selection, selectionArgs, null, null, null); if (cursor == null) { return null; } else if (!cursor.moveToFirst()) { cursor.close(); return null; } return cursor; } /** */ */ 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 records"); onCreate(db); } } public RecordsDbHelper(Context context) { mDbHelper = new DatabaseHelper(context); } /** * * @param data , * @return id , -1, */ public long createRecord(String data) { mDb = mDbHelper.getWritableDatabase(); ContentValues initialValues = new ContentValues(); initialValues.put(KEY_DATA, data); return mDb.insert(DATABASE_TABLE, null, initialValues); } }
ãããžã§ã¯ãå šäœã¯code.google.comã§ååŸã§ããŸãã
ãæž èŽããããšãããããŸããïŒ