2013-05-30 66 views
1

不止一個人遇到了如何爲使用SimpleCursorAdapter的ListView實現過濾器的問題。我遇到了這個問題,並且在這個網上找到了很多關於這個問題的答案。我從各處收集了一些代碼,直到我終於找到了工作,而且看起來更簡單。如何使用編輯文本和SimpleCursor適配器過濾ListView?

我見過的谷歌I/O 2010 - YouTube上的ListView視頻的世界,它說,你必須實現用getFilter方法並實現過濾的類適配器上,這樣的事情:

public class MyListCursorAdapter extends SimpleCursorAdapter implements Filterable{ 

... 
@Override 
public Filter getFilter() { 
    return new Filter(){ 
     @Override 
     protected FilterResults performFiltering(CharSequence charSequence){ 
      FilterResults results = new FilterResults(); 

      //If there's nothing to filter on, return the original data for your list 
      if(charSequence == null || charSequence.length() == 0){ 
       results.values = originalData; 
       results.count = originalData.getCount(); 
      } 
      else{ 

       Cursor filterResultsData = null; 

       filterResultsData = DB.getResults(charSequence); 

       results.values = filterResultsData; 
       results.count = filterResultsData.getCount(); 
      } 

      return results; 
     } 

     @Override 
     protected void publishResults(CharSequence charSequence, FilterResults filterResults){ 
      filteredData = (Cursor) filterResults.values; 
      if (filterResults.count > 0) { 
       notifyDataSetChanged(); 
      }else{ 
       notifyDataSetInvalidated(); 
      } 
     } 
    }; 
} 
} 

至少是我試圖做的。我已經看到使用ArrayAdapter而不是Cursor,效果很好。但在光標的情況下,它不起作用,至少不適合我。也許我做錯了什麼。所以我對於做什麼感到困惑。我知道我有一個編輯文本,一個列表視圖,每次在我的編輯文本上放一些文本,我希望我的列表得到過濾。

現在我要回答我的問題,因爲我想與大家分享我的解決方案。

回答

1

這是我做了什麼:

佈局:在ListView控件textFilterEnabled = 「真」:

<EditText 
    android:id="@+id/editTextSearch" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:hint="@string/search" > 
</EditText> 


<ListView 
    android:id="@id/android:list" 
    android:textFilterEnabled="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

注重機器人。

我ListActivity或ListFragment:

public class ClientesActivity extends ListFragment { 

private MyListCursorAdapter myAdapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.activity_clientes, container, false); 

      //call to a method that fill my list 
    myListGetFilled(view); 

      //Put a listener to edit text search 
    EditText etSearch = (EditText) view.findViewById(R.id.editTextSearch); 
    etSearch.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
      // When user changed the Text 
      myAdapter.getFilter().filter(cs); 

     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub       
     } 
    }); 


    return view; 
} 

    private void myListGetFilled(View view){ 

    Cursor mCursor = DB.fillMyList(getActivity()); 

    if(mCursor.getCount()>0){ 

     String[] from = new String[] { "name", "description"}; 
     int[] to = new int[] { R.id.textViewName, R.id.textViewDescription}; 

     ListView myListView = (ListView) view.findViewById (android.R.id.list); 

     myAdapter = new MyListCursorAdapter(context, R.layout.activity_row_list, mCursor, from, to); 
     myListView .setAdapter(myAdapter); 

    } 
} 

最後我的自定義光標適配器:

public class MyListCursorAdapter extends SimpleCursorAdapter implements Filterable{ 

    ... 

    /*THE METHOD THAT DOES THE MAGIC*/ 
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) { 

    if (getFilterQueryProvider() != null){ 
     return getFilterQueryProvider().runQuery(constraint); 
    } 

    Cursor filterResultsData = DB.getResults(constraint); 
    return filterResultsData; 
    } 
} 

就是這樣!。

希望這可以是有用的人。如果您認爲有比這更好的解決方案,請分享!!