2014-04-18 4 views
1

У меня есть фрагмент, который расширяет ListFragment. Этот фрагмент включает также элемент EditText, и я хочу отфильтровать список, когда его текст меняется.Отфильтровать список при изменении текста в ListFragment

Я думал о следующем коде:

EditText inputSearch = (EditText) getView().findViewById(R.id.inputGroupSearch); 
    inputSearch.addTextChangedListener(new TextWatcher() { 

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

Но, как-то getListAdapter() не имеет "getFilter()" метод. Я хотел бы получить лучшую идею, чтобы решить эту проблему.

Спасибо!

+0

Вы можете реализовать пользовательский fillter с помощью фильтруемого http://developer.android.com/reference /android/widget/Filterable.html – Raghunandan

+0

Могу ли я реализовать интерфейс Filterable для адаптера списка по умолчанию в ListFragment? Можете ли вы показать мне, как? или направить меня в подходящее место? – yogo

ответ

0

напечатанный материал адаптер возвращаемого getListAdapter в свой собственный пользовательского адаптер

filerText = (EditText) view.findViewById(R.id.filerText); 
filerText.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
     YourCustomAdapter adapter = (YourCustomAdapter)getListAdapter(); 
     adapter.getFilter().filter(cs); 
    } 

    @Override 
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } 

    @Override 
    public void afterTextChanged(Editable arg0) {} 
}); 
1

Попробуйте это:

lv.setAdapter(new ArrayAdapter<String>(getActivity(), 
      android.R.layout.simple_list_item_activated_1, list.TERM)); 
    lv.setTextFilterEnabled(true); 
    et.addTextChangedListener(new TextWatcher() 
    { 
     public void afterTextChanged(Editable s) 
    { 
     // Abstract Method of TextWatcher Interface. 
    } 
    public void beforeTextChanged(CharSequence s, 
    int start, int count, int after) 
    { 
    // Abstract Method of TextWatcher Interface. 
    } 
    public void onTextChanged(CharSequence s,int start, int before, int count) 
    { 
    textlength = et.getText().length(); 
    array_sort.clear(); 
    for (int i = 0; i < words.length; i++) 
    { 
     if (textlength <= words[i].length()) 
     { 
      if(et.getText().toString().equalsIgnoreCase((String)words[i].subSequence(0, textlength))) 
       { 
       array_sort.add(words[i]); 
       } 
     } 
    } 
    lv.setAdapter(new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_activated_1, array_sort)); 
    } 
    }); 
+0

Спасибо! Вы поможете мне с https://stackoverflow.com/questions/47974521/filter-data-by-input-in-custom-arrayadapter-displays-empty-items/47974785#47974785 –

Смежные вопросы