【发布时间】:2015-10-22 16:46:29
【问题描述】:
我正在 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);
【问题讨论】:
-
“我正在 Android 上实现内容提供程序”——您的代码看起来更像是在尝试使用由其他人编写的
ContentProvider。您是否尝试真正实现您的自己的ContentProvider? -
根据this answer,至少对于
UserDictionary,like ?应该确实有效。 -
汉诺之类的?结合使用 %..% 包装用户的输入。还要向 CommonsWare 大声喊叫,让我用精确的措辞保持诚实。