使用android Search Action查询多个列

时间:2015-07-30 01:27:02

标签: android android-sqlite greendao

我在Android世界中已经有几个月了,我真的很感谢你的帮助。

我正在尝试使用Android搜索建议,同时能够从我的数据库中查询两个表。例如,TableName =赞美诗。我想从HymnNumber& amp; 类别列。

我的问题: 解决方案一 - 下面的解决方案有效,但我的“搜索建议”不会显示。当我添加我的selectionArgs时,我也会收到错误(FYI在没有selectionArgs的情况下工作正常)。

String selection1 =
            Hymn_Number + " LIKE '%" + query + "%' OR "
            + Category + " LIKE '%" + query + "%'" ;

这是我得到的错误:

java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.

解决方案二:以下代码也可以正常工作(显示搜索建议),但不查询“第二列”。 (仅供参考我添加selectionArgs时没有错误)

        String selection2 = Hymn_Number + " LIKE ? " + " OR " + Category + " LIKE ? ";

以下完整代码:

/** Returns Hymns  */
public Cursor getHymns(String query, String[] selectionArgs){

    if(selectionArgs!=null){
        selectionArgs[0] = "%"+selectionArgs[0] + "%";
    }

    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setProjectionMap(mAliasMap);

    queryBuilder.setTables(hymnDao.getTablename());


    String Hymn_Number = HymnDao.Properties.HymnNumber.columnName;
    String Category = HymnDao.Properties.Category.columnName;


    //Working Code
    String selection1 =
            Hymn_Number + " LIKE '%" + query + "%' OR "
            + Category + " LIKE '%" + query + "%'" ;

    String selection2 = Hymn_Number + " LIKE ? " + " OR " + Category + " LIKE ? ";


    String[] projectionIn = {"_ID",
            SearchManager.SUGGEST_COLUMN_TEXT_1,
            SearchManager.SUGGEST_COLUMN_TEXT_2,
            SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID};


    Cursor c = queryBuilder.query(database,
            projectionIn,
            selection2,
            selectionArgs,
            null,
            null,
            Category + " asc ","10"
    );
    return c;

}

0 个答案:

没有答案