表演'喜欢' Android内容提供商

时间:2015-10-22 16:46:30

标签: java android

我在Android上实施内容提供商。我希望我的查询不仅仅是查找完全匹配 - 有没有办法指定'喜欢'查询内容提供程序查询方法?

当前代码。请注意,mSearchString是用户提供的字符串 - 我目前正在使用它来查询UserDictionary,并且它返回任何内容的唯一方法是mSearchString与单词完全匹配在UserDictionary。我希望能够匹配部分查询(例如,如果苹果,阿姨和实际在字典中,我想要搜索" a"并将它们全部返回)

        mSelectionClause = UserDictionary.Words.WORD + " = ?";

        // Moves the user's input string to the selection arguments.
        // Remember to add code here to check for invalid or malicious input.
        mSelectionArgs[0] = mSearchString;

    // Does a query against the table and returns a Cursor object
            Cursor mCursor = getContentResolver().query(
                    UserDictionary.Words.CONTENT_URI,  // The content URI of the words table
                    mProjection,                       // The columns to return for each row
                    mSelectionClause,                   // Either null, or the word the user entered
                    mSelectionArgs,                    // Either empty, or the string the user entered
                    mSortOrder);    

2 个答案:

答案 0 :(得分:3)

我正在使用SQLiteDatabase(不使用getContentResolver()但我不知道它是什么),但我可以搜索给定模式like的单词{pattern 1}})使用这段代码,因为语法看起来一样,我想它可以适合你:

    String whereClause = "word like ?";
    String[] wildCardReplacements = {pattern};
    cursor = database.query(TABLE_NAME, mColumns, whereClause,  
             wildCardReplacements, null, null, null);

因此,如果pattern包含%a%,我会得到"苹果,阿姨和实用的"还有很多其他人。

如果情况不明显,在您的情况下,像mSearchString = "%" + mSearchString + "%"这样的内容只允许用户输入a(例如)并获得所需的匹配。

P.S。 database被定义为android.database.sqlite.SQLiteDatabase

答案 1 :(得分:0)

  

100%的工作代码,将代码更改为这样的代码(光标)

Cursor contactsContractContacts = resolver.query(
ContactsContract.Contacts.CONTENT_URI, projection,
ContactsContract.Contacts.DISPLAY_NAME + " like ?",
new String[]{"%" + filterStr + "%"},
ContactsContract.Contacts.DISPLAY_NAME + " ASC");