2017-03-27 108 views
8

我在嘗試修改使用ActionBarSherlockSearchView$SearchAutoCompleteopen source dictionary project。例如,我有一個名爲「Google Play」的條目。當我輸入「Google」時,SearchView能夠返回建議,但如果我搜索「播放」,情況並非如此。如何使SearchView能夠搜索包含文本的條目?在SearchView中搜索包含特定字符的文本AutoCompleteTextView

 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

而這是Cursor查詢其中我found from here

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; 
    } 

這是locateFirst方法,該方法我found from here

/** 
    * 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

+1

你發佈的代碼沒有顯示任何過濾跡象。應該有一個類或一些代碼,根據來自'searchView'的輸入字符串進行過濾。查找從'searchView'獲取字符串的代碼,並將其作爲搜索查詢傳遞,可能在'onTextChanged'回調中,並將其代碼和任何與其相關的代碼發佈以進一步幫助您。 – ahasbini

+0

添加您的搜索代碼。它沒有足夠的代碼。 – ADM

+0

我編輯了我的問題。 – user2872856

回答

-1

上TextChanged事件使用的字符串的方法。載。然後在該方法的基礎上執行所需的搜索。我希望這會幫助你

+0

我已更新我的問題。請看一看。 – user2872856

相關問題