2011-10-28 7 views
1

私はフィルタをかけたリストビューと、テキスト変更イベントを取得するためのTextWatcherを持っているEditTextを持っています。編集テキストからテキストを削除するまで、すべてがうまくいきます。フィルタは最初の文字のままで、フィルタには戻りません。ListViewの空のテキストフィルタ

私は間違っていますか?ここにTextWatcherのコードがありますが、私はフィルターを無効にしようとしましたが効果はありませんでした。あなたの助けの代わりにフィルタリングListView

答えて

2

iはアディルSoomroの答えは悪いことだと思う...私はただ見てandroid sourceとsetFilterTextのようになります。

public void setFilterText(String filterText) { 
    // TODO: Should we check for acceptFilter()? 
    if (mTextFilterEnabled && !TextUtils.isEmpty(filterText)) { 
     createTextFilter(false); 
     // This is going to call our listener onTextChanged, but we might not 
     // be ready to bring up a window yet 
     mTextFilter.setText(filterText); 
     mTextFilter.setSelection(filterText.length()); 
     if (mAdapter instanceof Filterable) { 
      // if mPopup is non-null, then onTextChanged will do the filtering 
      if (mPopup == null) { 
       Filter f = ((Filterable) mAdapter).getFilter(); 
       f.filter(filterText); 
      } 
      // Set filtered to true so we will display the filter window when our main 
      // window is ready 
      mFiltered = true; 
      mDataSetObserver.clearSavedState(); 
     } 
    } 
} 

あなたはそれがフィルターとアダプタの設定だ見ることができるように...

あなたはlv.clearTextFilter();代わりに感謝

+0

はい、この回答は最後よりも完全です。ご協力いただきありがとうございます。 –

+0

+1 - あなたに同意する@Selvin。 –

1

ため

EditText txtFilter = (EditText) this.findViewById(R.id.txtFilter); 
txtFilter.addTextChangedListener(new TextWatcher() { 
    public void afterTextChanged(Editable s) { 
     if(s.length()==0) 
     { 
      lv.setTextFilterEnabled(false); 
     } 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) 
    { 
    } 

    public void onTextChanged(CharSequence s, int start, int before, int count) 
    { 
     lv.setTextFilterEnabled(true); 
     lv.setFilterText(s.toString()); 
    } 
}); 

おかげで、この方法のようAdapterでろ過ん:

txtFilter.addTextChangedListener(new TextWatcher() { 
    public void afterTextChanged(Editable s) {   
    } 
    public void beforeTextChanged(CharSequence s, int start, int count, int after){ 
    } 
    public void onTextChanged(CharSequence s, int start, int before, int count){ 
     adapter.getFilter().filter(s); 
    } 
}); 
+0

lv.setTextFilterEnabled(false);のを使用する必要があります!それがそれを解決しました。 –

+0

これは問題ではありません...リストビューのsetFilterTextアダプターの下敷きのフィルターも設定 – Selvin

+0

遅れて来た方に。 @Selvinによって提供される回答は正しいことに注意してください。 –