【问题标题】:Android search suggestion, why have to type the whole word for suggestion?Android搜索建议,为什么必须输入整个单词才能获得建议?
【发布时间】:2014-04-01 10:42:27
【问题描述】:

我有一个启用了建议的搜索小部件。一切正常,但我必须写下整个单词才能获得建议。

我已经设置了 - android:searchSuggestThreshold="2",但仍然需要完成一个单词 (5-6) 个字符,以获得建议。为什么?

我真的非常感谢一些帮助。 谢谢!

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exploreca.tourfinder"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.CALL_PHONE" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_exploreca"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >     
        <activity
            android:name="com.exploreca.tourfinder.MainActivity"
            android:label="@string/app_name" >            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>            
        </activity>
        <activity 
            android:name=".SettingsActivity">            
        </activity>
        <activity 
            android:name=".TourDetailActivity">            
        </activity>        
        <!-- Search results activity -->
        <activity android:name=".SearchActivity">
             <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter> 
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>  

        <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchActivity" />

        <provider
        android:name=".SearchContentProvider"
         android:authorities="com.exploreca.tourfinder.SearchContentProvider" >
     </provider>   

    </application>
</manifest>

在 SearchContentProvider 中使用的部分:

@Override
public Cursor query(Uri uri, String[] projection, String selection, 
          String[] selectionArgs, String sortOrder) { 

    Log.i(LOGTAG, "The typed characters are: " + selectionArgs[0]);

        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(ToursDBOpenHelper.TABLE_TOURS);           

        HashMap<String, String> columnMap = new HashMap<String, String>();
        columnMap.put(BaseColumns._ID, ToursDBOpenHelper.COLUMN_ID + " AS " + BaseColumns._ID);
        columnMap.put(SearchManager.SUGGEST_COLUMN_TEXT_1, ToursDBOpenHelper.COLUMN_TITLE + " AS " + SearchManager.SUGGEST_COLUMN_TEXT_1);
        columnMap.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA, ToursDBOpenHelper.COLUMN_ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA);
        builder.setProjectionMap(columnMap);

        dbhelper = new ToursDBOpenHelper(getContext());
        //database = dbhelper.getReadableDatabase();

        SQLiteDatabase db = dbhelper.getWritableDatabase();
        cursor = builder.query(db, projection, selection, selectionArgs, null, null, null, null);

        return cursor;
}

还有searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="@string/search_hint"
    android:label="@string/app_name" 
    android:searchSuggestAuthority="com.exploreca.tourfinder.SearchContentProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestSelection="city LIKE ?" 
    android:searchSuggestThreshold="2"
    android:includeInGlobalSearch="true"/>

【问题讨论】:

    标签: android android-contentprovider search-suggestion


    【解决方案1】:

    感谢post,我已经弄明白了。要在输入第一个或第二个单词后出现建议,我们需要“通配符”:

    "city LIKE '%' || ? || '%'"
    

    【讨论】:

      猜你喜欢
      • 2011-04-04
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 2012-07-10
      • 1970-01-01
      相关资源
      最近更新 更多