2014-12-06 35 views
0

我創建一個帶過濾器的ListView。事情是,過濾器正在工作,但我需要按搜索按鈕,以便可以過濾列表。我想避免這種情況,並在我輸入字母時進行自動過濾。Android:ListView過濾器沒有按下搜索按鈕

下面是活動的執行:

private void setupSearchView() { 

    filter = navAdapter.getFilter(); 

    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
    search.setSubmitButtonEnabled(false); 
    search.setOnQueryTextListener(this); 
    search.setIconified(true); 


    int searchSrcTextId = getResources().getIdentifier("android:id/search_src_text", null, null); 
    EditText searchEditText = (EditText) search.findViewById(searchSrcTextId); 
    searchEditText.setTextColor(Color.WHITE); 
    searchEditText.setHintTextColor(Color.LTGRAY); 
} 

@Override 
public boolean onQueryTextSubmit(String query) { 
     filter.filter(query); 
     return true; 

} 

@Override 
public boolean onQueryTextChange(String newText) { 
    return false; 
} 

適配器:

@Override 
public Filter getFilter() { 
    /** 
    * A filter object which will 
    * filter message key 
    * */ 
    Filter filter = new Filter() { 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 

      list = (List<NavigationDrawerListModel>) results.values; // has the filtered values 
      notifyDataSetChanged(); // notifies the data with new filtered values. Only filtered values will be shown on the list 
     } 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults();  // Holds the results of a filtering operation for publishing 

      List<NavigationDrawerListModel> FilteredArrList = new ArrayList<NavigationDrawerListModel>(); 

      if (mList == null) { 
       mList = new ArrayList<NavigationDrawerListModel>(list); // saves the original data in mOriginalValues 
      } 

      if (mListItem == null) { 
       mListItem = new ArrayList<String>(); 
       for (NavigationDrawerListModel recipe : mList) { 
        mListItem.add(recipe.getTitle()); 
       } 
      } 

      /** 
      * 
      * If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values 
      * else does the Filtering and returns FilteredArrList(Filtered) 
      * 
      **/ 

      if (constraint == null || constraint.length() == 0) { 

       /* CONTRACT FOR IMPLEMENTING FILTER : set the Original values to result which will be returned for publishing */ 
       results.count = mList.size(); 
       results.values = mList; 
      } else { 
       /* Do the filtering */ 
       constraint = constraint.toString().toLowerCase(); 

       for (int i = 0; i < mListItem.size(); i++) { 
        String data = mListItem.get(i); 
        if (data.toLowerCase().startsWith(constraint.toString())) { 
         FilteredArrList.add(mList.get(i)); 
        } 
       } 

       // set the Filtered result to return 
       results.count = FilteredArrList.size(); 
       results.values = FilteredArrList; 
      } 
      return results; 
     } 
    }; 
    return filter; 
} 

和XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" 
    > 

    <!-- Toolbar --> 

    <include layout="@layout/toolbar" /> 

    <!-- content --> 
    <android.support.v4.widget.DrawerLayout 
     android:layout_width="match_parent" 
     android:id="@+id/drawerLayout_test" 
     android:layout_height="match_parent"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:background="#fff" 
      android:layout_height="match_parent"> 

      <TextView 
       android:layout_centerInParent="true" 
       android:layout_width="wrap_content" 
       android:textColor="#000" 
       android:text="Our Content" 
       android:layout_height="wrap_content" /> 
     </RelativeLayout> 
     <!-- nav drawer --> 
     <LinearLayout 
      android:layout_gravity="start" 
      android:orientation="vertical" 
      android:background="@color/black" 
      android:layout_width="280dp" 
      android:layout_height="match_parent" 
      > 

      <android.widget.SearchView 
       android:id="@+id/searchViewHomePage_test" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:focusable="false" 
       android:iconifiedByDefault="false" 
       android:queryHint="@string/abc_searchview_description_search" 

       /> 


      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       > 

       <ListView 
        android:id="@+id/drawer_list_items_test" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_gravity="start" 
        android:choiceMode="singleChoice" 
        android:dividerHeight="1dp" 
        /> 

       </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_marginTop="0.1dp"> 

      <ListView 
       android:id="@+id/drawer_list_test" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="start" 
       android:choiceMode="singleChoice" 
       android:dividerHeight="1dp" 
       /> 

     </LinearLayout> 


     </LinearLayout> 

    </android.support.v4.widget.DrawerLayout> 

</LinearLayout> 

回答

0

使用SearchView#setOnQueryTextListener和在OnQueryTextListenerboolean onQueryTextChange(String newText)方法執行濾波。所以在你的情況下,將filter.filter(query);onQueryTextSubmit移至onQueryTextChange

+0

太棒了!這解決了。感謝您的快速響應。 – 2014-12-06 17:08:44

+0

沒問題。我很高興能夠提供幫助。請接受答案。 – 2014-12-06 17:11:08