如何用2个适配器创建android自动完成?

时间:2016-02-16 05:13:12

标签: android autocomplete autocompletetextview

enter image description here

enter image description here

如何用另一个适配器将适配器1的阈值(0)替换为阈值(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);
}

最好的问候..

1 个答案:

答案 0 :(得分:0)

Lukman,请告诉我的代码。

最终的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;
        }
    });