2016-02-16 97 views
0

enter image description here如何用2個適配器創建android自動完成?

enter image description here

我怎樣才能更換閾值(0)與適配器1〜閾值(1)與另一適配器? 請幫助.. 這是我的代碼,它不工作:

String[] hashtag = {"Android", "Andaa", "Java", "XML", "OS", "SQL", "IOS"}; 
String[] hashtag_server = { "Ajax", "Php", "Javascript"}; 

和內部類onCreateOptionsMenu:

final SearchView.SearchAutoComplete searchAutoComplete = 
    (SearchView.SearchAutoComplete) 
    mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); 
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
     android.R.layout.simple_dropdown_item_1line, hashtag); 
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, 
     android.R.layout.simple_dropdown_item_1line, hashtag_server); 

if(searchAutoComplete.getThreshold() == 0) { 
searchAutoComplete.setAdapter(adapter); 
}else if(searchAutoComplete.getThreshold() > 0){ 
searchAutoComplete.setAdapter(adapter2); 
} 

最好的問候..

+0

你的問題不清楚。使用兩個不同閾值的適配器,您想實現什麼? –

+0

假設:我想顯示標籤(#android,#蘋果)閾值(0),但在同樣的情況下,爲閾值(1),我想顯示自動完成。我該怎麼做?對不起,我的英文不好 – Lukmanpryg

+0

使用[TextWatcher](http://developer.android.com/reference/android/text/TextWatcher.html)爲你的AutoTepleteTextView在beforeTextChanged檢查第一個字母,'getText()。toString() .length == 0'然後設置不同的適配器並更改閾值,否則將適配器更改爲先前的。 –

回答

0

盧克曼,請CEK我的代碼。

最終的SearchManager searchManager = (SearchManager)getActivity()。getSystemService(Context.SEARCH_SERVICE);

// Retrieves the SearchView from the search menu item 
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); 

    // Assign searchable info to SearchView 
    searchView.setSearchableInfo(
      searchManager.getSearchableInfo(getActivity().getComponentName())); 

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextSubmit(String queryText) { 
      // Nothing needs to happen when the user submits the search string 
      return true; 
     } 

     @Override 
     public boolean onQueryTextChange(String newText) { 
      ListContactBotFragment.this.adapter.getFilter().filter(newText); 
      return true; 
     } 
    }); 
+0

我已經解決了這個問題。無論如何都要反駁:D – Lukmanpryg

相關問題