Пустой текстовый фильтр в ListView - PullRequest
1 голос
/ 28 октября 2011

У меня есть ListView, с помощью которого я фильтрую, и EditText, на котором у меня есть TextWatcher, чтобы получать события изменения текста. Все прекрасно работает, пока вы не удалите текст из текста редактирования. Фильтр остается первой буквой и не возвращается к фильтру.

Что я делаю не так? Вот код для TextWatcher, я даже пытался отключить фильтр, но это не имело никакого эффекта.

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());
    }
});

Спасибо за вашу помощь

Ответы [ 2 ]

2 голосов
/ 28 октября 2011

я думаю, что ответ Адиля Соомро плохой ... я просто смотрю на Android-источник и 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(); вместо lv.setTextFilterEnabled(false);

1 голос
/ 28 октября 2011

Вместо фильтрации ListView выполните фильтрацию через 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);
    }
});
...