2014-03-30 106 views
0

我有一個簡單的搜索小部件,我很難在我的應用程序中實現自定義搜索建議功能。空白/空白下拉,android自定義搜索建議

關於「Building a suggestion table」的Google Api文檔實際上記錄不準確。到目前爲止,我設法讓我的「SearchContentProvider」工作,它會返回建議,但是當我在下拉列表中輸入時,我只能看到「空白」字段並且沒有文本。

有人可以建議我如何建立建議表嗎?我應該在哪裏實施「SUGGEST_COLUMN_TEXT_1」以格式化並獲得我的建議?

enter image description here

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; 
    } 

} 
+1

它是返回的建議光標的列 – pskink

回答

0

我已經想通了。即使我已將「SUGGEST_COLUMN_TEXT_1」作爲新列添加到我的表格中,但無論我將它放在什麼位置,它仍然是空的。

因此,我已經從數據庫表中刪除它,並以HashMap的形式將其添加到我的ContentProvider中,該HashMap由一組鍵和值對組成。在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; 
     } 

... 
1

如果你還在尋找一個解決方案,你需要使用SearchManager.SUGGEST_COLUMN_TEXT_1而不是SUGGEST_COLUMN_TEXT_1,如said in this answer