Android - 带有自定义建议的搜索对话框

标签 android search

我有一个搜索对话框,需要从数据库中启用建议,如此处所示 http://developer.android.com/images/search/search-suggest-custom.png .

当我单击操作栏菜单项时,我的搜索对话框将显示在顶部。当用户开始输入一些文本时,它应该搜索数据库并相应地显示建议。到目前为止,我有一个搜索对话框,但是当用户输入一些文本时,没有显示任何建议。

我的内容提供者:

public class MyCustomSuggestionProvider extends ContentProvider {
    SQLiteDatabase db;
    SQLiteQueryBuilder qb;
    String sqlTables = "countries";

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public boolean onCreate() {

        return false;
    }
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        MyDatabase myDB = new MyDatabase(getContext());
        db = myDB.getReadableDatabase();
        Cursor c = null;

        if (selectionArgs != null && selectionArgs.length > 0
                && selectionArgs[0].length() > 0) {

            qb = new SQLiteQueryBuilder();
            String[] sqlSelect = { "name" };
            qb.setTables(sqlTables)

                c = qb.query(db, null, selection, selectionArgs, null, null,
                        null);
                c.moveToFirst();
        } else {
            return null;
        }
        return c;
    }
    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        return 0;
    }

}

我的 list :

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/Theme.Visa" >
            <provider
                android:name="com.m7.visa.contentprovider.MyCustomSuggestionProvider"
                android:authorities="com.m7.visa.contentprovider.MyCustomSuggestionProvider" >
            </provider>

            <activity
                android:name="com.m7.visa.MainActivity"
                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>

我的可搜索配置:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="hint..."
    android:label="@string/app_name"
    android:searchSuggestAuthority="com.m7.visa.contentprovider.MyCustomSuggestionProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestSelection=" ?" >

</searchable>

知道我哪里出错了吗?

最佳答案

问题出在问号 android:searchSuggestSelection="?">。当您在搜索 View 中键入“something”时,它会传递“something”,空格在左侧。所以解决方案是删除空格,但是 '?'是 Android XML 文件中的特殊字符,这意味着您必须对它进行转义。

所以最终的解决方案是 android:searchSuggestSelection="\?">

关于Android - 带有自定义建议的搜索对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15878168/

相关文章:

Android - 在相机顶部绘制 OpenGL 图像

android - ImageView 使应用程序崩溃

java - 如何使用光标获取项目的位置并删除它?

search - Kibana:在文本中搜索字符串

python - 正则表达式Python同时查找美元金额和几个单词

Android线程等待直到可见

java - 用于排队最大数量元素的最有效数据存储对象

search - Google如何如此快速地(针对如此多的文档)执行搜索(针对任何给定的查询),并且仍然设法自定义结果?

python - 如何识别 Pandas DataFrame 中列的行中的字符串重复?

javascript - 只返回匹配的一部分