2016-03-04 99 views
2

我正在我的應用程序的操作欄中進行搜索選項。我有搜索圖標顯示,當我點擊它並且我可以鍵入查詢時它會摺疊。Android SearchView setOnQueryTextListener不工作?

當我的應用程序啓動時,onCreateOptionsMenu被調用。我打印一個測試語句來驗證它,然後在該函數中設置一個setOnQueryTextListener。但它不起作用。當我在searchview中輸入內容時,我試圖通過打印來測試它,但它不起作用。請幫我找到錯誤。另外還有一個Listener,用於擴展和拼合,但也不起作用。

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 

    inflater.inflate(R.menu.manageusersmenu, menu); 
    MenuItem item = menu.findItem(R.id.action_search); 
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(item); 

    Log.i("onCreateOptionsMenu: ", "I made it in here"); 

    MenuItemCompat.setOnActionExpandListener(item, 
      new MenuItemCompat.OnActionExpandListener() { 
       @Override 
       public boolean onMenuItemActionCollapse(MenuItem item) { 
        mAdapter.setFilter(mUsers); 
        Log.i("ActionCollapse: ", "Collapsed"); 
        return true; 
       } 
       @Override 
       public boolean onMenuItemActionExpand(MenuItem item) { 
        Log.i("ActionExpand: ", "Expanded"); 
        return true; 
       } 
      }); 

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextSubmit(String query) { 
      return false; 
     } 

     @Override 
     public boolean onQueryTextChange(String newText) { 
      Log.i("on query: ", newText); 
      List<User> filteredModelList = filter(mUsers, newText); 
      for (int i = 0; i < filteredModelList.size(); i++) { 
       Log.i("user, ", filteredModelList.get(i).getFName().toString()); 
      } 
      mAdapter.setFilter(filteredModelList); 
      return true; 
     } 

     private List<User> filter(List<User> users, String query) { 
      query = query.toLowerCase(); 

      final List<User> filteredModelList = new ArrayList<>(); 
      for (User u : users) { 
       final String text = u.getFName().toLowerCase(); 
       if (text.contains(query)) { 
        filteredModelList.add(u); 
       } 
      } 
      return filteredModelList; 
     } 
    }); 

} 

菜單文件

<menu 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    > 

    <!-- "Mark Favorite", should appear as action button if possible --> 
    <item 
     android:id="@+id/add_new_user" 
     android:icon="@drawable/ic_action_name" 
     android:title="@string/add_new_user" 
     app:showAsAction="ifRoom"/> 

    <item 
     android:id="@+id/action_search" 
     android:icon="@android:drawable/ic_menu_search" 
     android:title="action_search" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:showAsAction="always|collapseActionView" /> 

</menu> 
+0

發表您的manageusersmenu.xml也 – UMESH0492

+0

剛剛提出的菜單文件 – Pete

回答

3

我用這個和它的工作不知道爲什麼:

Android abs with SearchView, onQueryTextListener not working

@Override 
    public void onPrepareOptionsMenu(Menu menu) { 
     super.onPrepareOptionsMenu(menu); 
     MenuItem item = menu.findItem(R.id.action_search); 
     SearchView searchView = (SearchView) MenuItemCompat.getActionView(item); 

     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
      @Override 
      public boolean onQueryTextSubmit(String query) { 
       return false; 
      } 

      @Override 
      public boolean onQueryTextChange(String newText) { 
       Log.i("well", " this worked"); 
       return false; 
      } 
     }); 
    }