2017-04-11 58 views
0

我正在使用由Miguel Catalan從this repo開發的材料搜索視圖。我有建議列表,當用戶選擇建議時,他/她應該看到過濾結果。我的問題在文本寫入事件或選項選擇上沒有任何反應。我在我的主要活動文件中添加了這個: -在MaterialSearchView中的OnClick項目選擇android

MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view); 
    //searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions)); 
    String [] suggestions = {"Item1","Item2","Item3"}; 
    searchView.setSuggestions(suggestions); 
    searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextSubmit(String query) { 
      Log.d("here",query); 
      return false; 
     } 

     @Override 
     public boolean onQueryTextChange(String newText) { 
      Log.d("here",newText); 

      return false; 
     } 

    }); 

    searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Log.d("ItemClick","onItemClick,"); 
     } 
    }); 

    searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() { 
     @Override 
     public void onSearchViewShown() { 
      Log.d("here","search"); 
     } 

     @Override 
     public void onSearchViewClosed() { 
      Log.d("here","search"); 
     } 
    }); 

我沒有看到在調試日誌,這意味着沒有方法被稱爲什麼。任何人都可以告訴這可能是什麼原因嗎? enter image description here }

回答

1
searchView.setOnItemSelectedListener(new AdapterView.OnItemClickListener() { 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     Log.d("ItemClick","onItemClick,"); 
    } 
}); 

請改用onitemclickListener的onitemSelectedListener。希望這可以幫助。

+0

MaterialSearchView沒有setOnItemSelectedListener()方法只是setOnItemClickListener(),所以不能使用此:\ – user2714823

+0

[鏈接] https://material.io/guidelines/patterns/selection.html 檢查此鏈接。 – jagteshwar751

0

用於搜索查看設置適配器,OnItemClick將工作

mSearchView.setAdapter(new SearchAdapter(this, results)); 

有了結果爲String []和 必須導入SearchAdapter import com.miguelcatalan.materialsearchview.SearchAdapter;