【问题标题】:Search for text which contains certain character in SearchView AutoCompleteTextView在 SearchView AutoCompleteTextView 中搜索包含特定字符的文本
【发布时间】:2017-08-19 21:25:19
【问题描述】:

我正在尝试修改使用ActionBarSherlock SearchView$SearchAutoCompleteopen source dictionary project。例如,我有一个名为“Google Play”的条目。 SearchView 能够在我输入“Google”时返回建议,但如果我搜索“Play”则不是这样。如何使SearchView能够搜索到包含该文本的条目?

        public boolean onQueryTextChange(String newText) {
            if (MdxDictBase.isMdxCmd(newText)){
                currentEntry.setEntryNo(DictEntry.kSystemCmdEntryNo);
            }
            if (!skipUpdateEntryList) {
                dict.locateFirst(newText, true, true, true, false, currentEntry);
                syncHeadwordList();
            }
            skipUpdateEntryList = false;
            return true; //return false if want the system provide a suggestion list?
        }

完整代码可以found here

这是我found from hereCursor 查询。

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Log.d(TAG, "Got query:" + uri.toString());
        if (uri.getPath() != null && uri.getPath().startsWith(SEARCH_PATH)) {
            if (fCurrentDict != null && fCurrentDict.isValid()) {
                String query=uri.getLastPathSegment();
                if (query!=null && query.length()>0){
                    DictEntry entry = new DictEntry(0, "", fCurrentDict.getDictPref().getDictId());
                    if (query != null && query.length() > 0)
                        fCurrentDict.locateFirst(query, true, false, true, false, entry);
                    if (entry.isValid()) {
                        String limit = uri.getQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT);
                        int maxResultCount = 20;
                        if (limit != null && limit.length() > 0) {
                            try {
                                maxResultCount = Integer.parseInt(limit);
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                        }
                        ArrayList<DictEntry> entryList = new ArrayList<DictEntry>();
                        fCurrentDict.getEntries(entry, maxResultCount, entryList);
                        String[] columns = new String[]{
                                BaseColumns._ID,
                                SearchManager.SUGGEST_COLUMN_TEXT_1,
                                SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID,
                                SearchManager.SUGGEST_COLUMN_SHORTCUT_ID};
                        MatrixCursor cursor = new MatrixCursor(columns, maxResultCount);
                        Object[] row;
                        for (DictEntry cur_entry : entryList) {
                            String intentDataId = String.format("%d_%d_%s", cur_entry.getDictId(), cur_entry.getEntryNo(), cur_entry.getHeadword());
                            row = new Object[]{cur_entry.hashCode(), cur_entry.getHeadword(), intentDataId, SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT};
                            cursor.addRow(row);
                        }
                        return cursor;
                    }
                }
            }
        }
        return null;
    }

这是我found from herelocateFirst方法。

/**
     * Locates the first entry that match the specified headword
     *
     * @param headword     Headword to be searched for
     * @param convertKey   Convert the given headword into different Chinese form according to dictionary settings?
     * @param partialMatch Match partial of the headword. For example "abc" can be matched with "abd" headword search
     * @param entry        The matched entry if found
     * @return Return kMdxSuccess when succeed, otherwise return error codes.
     */
    public synchronized int locateFirst(String headword, boolean convertKey, boolean partialMatch, boolean startWithMatch, boolean bestMatch, DictEntry entry) {
        if (isValid()) {
            return locateFirstN(headword, convertKey, partialMatch, startWithMatch, bestMatch, entry);
        } else {
            return kMdxDatabaseNotInited;
        }
    }

这是我认为负责显示建议列表的适配器: https://bitbucket.org/raymanzhang/mdict-android-opensource/src/eba7b9b4ead17a5b3e027da4a37dbd4ee1162596/src/cn/mdict/widgets/MdxAdapter.java?at=master&fileviewer=file-view-default

【问题讨论】:

  • 您发布的代码没有任何过滤迹象。应该有一个类或一些代码根据来自searchView 的输入字符串进行过滤。查找从searchView 获取字符串并将其作为搜索查询传递的代码,可能在onTextChanged 回调中,并发布它的代码和任何与之相关的代码以进一步帮助您。
  • 添加您的搜索代码。它的代码不够。
  • 我已经编辑了我的问题。
  • 好吧,我认为您应该使用正则表达式来查询类似于 (% WildCard) 的内容提供者。我猜您无法控制查询,因为它是库的一部分。因此,当您将查询字符串传递给 CP 时,首先将其设为模式。 Exa :- 通过 "br" -> [[a-z]*br[a-z]*] 。我不确定,但我认为值得一试。
  • @ADM ,你有实现的例子吗?

标签: android actionbarsherlock searchview


【解决方案1】:

在 TextChanged 事件上使用 .Contains 方法的字符串。然后根据该方法执行所需的搜索。希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2012-12-05
    相关资源
    最近更新 更多