1

我正在尝试在 Android 中做一个 Suggestion Provider。根据我输入的文本显示建议(这部分已经完成),但是当我想从内容提供者恢复数据(它的_id)时,getData() 指令返回 null。

在 SuggestionProvider 中,我给 SUGGEST_COLUMN_TEXT_1、SUGGEST_COLUMN_TEXT_2 和 SUGGEST_COLUMN_INTENT_DATA_ID 赋值:

HashMap<String,String> map = new HashMap<String,String>();
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables("place");
Map<String, String> projectionMap = new HashMap<String, String>(); 
projectionMap.put(Place.COL_NAME,  Place.COL_NAME+" AS " + SearchManager.SUGGEST_COLUMN_TEXT_1); 
projectionMap.put(Place.COL_ADDRESS,  Place.COL_ADDRESS+" AS " + SearchManager.SUGGEST_COLUMN_TEXT_2); 
projectionMap.put("_id", "_id" + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID); 
projectionMap.put("_id", "_id");  
builder.setProjectionMap(projectionMap); 

在 SearchableActivity 我使用 getDataString() 或 getData() 但它们返回 null:

private void handleIntent(Intent intent) 
{ 
    if (Intent.ACTION_VIEW.equals(intent.getAction()))  
    { 
        Log.w("test",intent.getDataString()+"a");
    }
    else if (Intent.ACTION_SEARCH.equals(intent.getAction())) 
    { 
        ...
    } 
}
4

1 回答 1

0

最后,经过尝试,尝试和尝试,我解决了它。

我不知道为什么,但如果我写“projectionMap.put("_id", "_id"+" AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA)",getData() 返回 null 但是当我使用 "projectionMap.put("idPlace" , "idPlace"+" AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA); " getData() 返回我想要的。

这是我的最终代码:

SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables("Place");
Map<String, String> projectionMap = new HashMap<String, String>(); 
projectionMap.put(Place.COL_NAME,  Place.COL_NAME+" AS " + SearchManager.SUGGEST_COLUMN_TEXT_1); 
projectionMap.put(Place.COL_ADDRESS,  Place.COL_ADDRESS+" AS " + SearchManager.SUGGEST_COLUMN_TEXT_2); 
projectionMap.put("idPlace",  "idPlace"+" AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA); 
projectionMap.put("_id", "_id");  
builder.setProjectionMap(projectionMap); 

SQLiteDatabase db = clidbh.getWritableDatabase();

String[] fields = {"idPlace AS _id","name","address","idPlace"};

String where = "name like '%"+selectionArgs[0]+"%'";

row = builder.query(db, fields, where, null, null, null, "_id","3");

if (row == null) {
    return null;
} else if (!row.moveToFirst()) {
    row.close();
return null;
}
于 2014-06-03T18:10:18.677 回答