2

我有一个简单的搜索小部件,我很难在我的应用程序中实现自定义搜索建议功能。

关于“构建建议表”的 Google Api 文档的文档记录确实很差。到目前为止,我已经设法让我的“SearchContentProvider”工作,它返回建议,但是当我在下拉列表中输入时,我只得到“空白”字段而没有文本。

有人可以建议我如何构建建议表吗?我应该在哪里实施“SUGGEST_COLUMN_TEXT_1”以便格式化并获得我的建议?

在此处输入图像描述

SearchContentProvider.java:

package com.exploreca.tourfinder;

import android.content.ContentProvider;
import android.content.ContentValues;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import android.net.Uri;
import android.util.Log;

public class SearchContentProvider extends ContentProvider {

    SQLiteOpenHelper dbhelper;
    SQLiteDatabase database;        
    Cursor cursor; 
    private String LOGTAG;


    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public String getType(Uri arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri arg0, ContentValues arg1) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        return false;
    }

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

        //instantieting the datasouce
        dbhelper = new ToursDBOpenHelper(getContext());
        database = dbhelper.getReadableDatabase();
        Log.i(LOGTAG, "Selection Args: " + selectionArgs[0] + "\n " + "selection is: " + selection);        

        cursor = database.query(ToursDBOpenHelper.TABLE_TOURS, null, 
                selection, selectionArgs, null, null, null);

        Log.i(LOGTAG, "Returned" + cursor.getCount() + "rows");

        if (cursor.getCount() > 0){
            while(cursor.moveToNext()){         

                cursor.getColumnIndex(cursor.getString(cursor.getColumnIndex(ToursDBOpenHelper.COLUMN_CITY)));
                Log.i(LOGTAG, "Cursor is" + cursor);
            }           
        } 
            return cursor;      
    }


    @Override
    public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
        // TODO Auto-generated method stub
        return 0;
    }

}
4

2 回答 2

3

If you still looking for a solution, you need to use SearchManager.SUGGEST_COLUMN_TEXT_1 instead of SUGGEST_COLUMN_TEXT_1, as said in this answer.

于 2015-02-27T23:03:16.773 回答
0

我已经想通了。即使我已将“SUGGEST_COLUMN_TEXT_1”作为新列添加到我的表中,它仍然是空的,无论我在其中放入了什么。

所以我把它从数据库表中删除,并以 HashMap 的形式添加到我的 ContentProvider 中,它由一组键和值对组成。在 HashMap 中,每个键都映射到一个值。

我花了几天时间弄清楚这一点,希望可以帮助其他人使用工作代码:

...

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

                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, sortOrder, null);

                return cursor;
        }

...
于 2014-04-01T08:39:11.317 回答