#android #filter #expandablelistview
#Android #Фильтр #расширяемый просмотр списка
Вопрос:
Я уже реализовал представление списка с фильтром поиска, но прямо сейчас мне нужно изменить его на расширяемый вид списка с дочерним фильтром поиска. В качестве строки поиска был бы отредактированный текст, который фильтровал бы все дочерние элементы всех групп. Это сценарий,
* Человек — объект состоит из имени, адреса, номера телефона и фотографии.
Группа 1 — Друзья (ребенок из 1 человека, ребенок из 2 человек, ребенок из 3 человек)
Группа 2 — Семья (дочерний элемент из 1 человека, дочерний элемент из 2 человек)
Группа 3 — Соседи по офису (ребенок 1 человек, ребенок 2 человека, Ребенок 3 человека, ребенок 4 человека)
На данный момент мне приходится переносить из адаптера массива в базовый адаптер расширяемого списка и фильтровать. Кто-нибудь может мне помочь с этим? Спасибо.
Ответ №1:
edit = (EditText)findViewById(R.id.editText1);
edit.addTextChangedListener(filterTextWatcher);
private TextWatcher filterTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
((Filterable) ((ListAdapter) Adapter)).getFilter().filter(edit.getText().toString());
}
};
public class ListAdapter extends BaseExpandableListAdapter implements Filterable {
public void notifyDataSetInvalidated() {
super.notifyDataSetInvalidated();
}
public Filter getFilter() {
if (filter == null)
filter = new MangaNameFilter();
return filter;
}
private class MangaNameFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// NOTE: this function is *always* called from a background thread, and
// not the UI thread.
constraint = edit.getText().toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null amp;amp; constraint.toString().length() > 0) {
detailsList = detailsSer.GetAlldetails();
dupCatList = detailsList;
ArrayList<detailsEntity> filt = new ArrayList<detailsEntity>();
ArrayList<detailsEntity> lItems = new ArrayList<detailsEntity>();
synchronized(this) {
lItems.addAll(dupCatList);
}
for(int i = 0, l = lItems.size(); i < l; i ) {
detailsEntity m = lItems.get(i);
if (m.description.toLowerCase().contains(constraint))
filt.add(m);
}
result.count = filt.size();
result.values = fi<
} else {
detailsList = detailsSer.GetAlldetails();
dupCatList = detailsList;
synchronized(this) {
result.count = dupCatList.size();
result.values = dupCatList;
}
}
return resu<
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults result) {
// NOTE: this function is *always* called from the UI thread.
filtered = (ArrayList<detailsEntity>)result.values;
ArrayList<Integer> IdList = new ArrayList<Integer>();
IdList.clear();
for(int i = 0; i < filtered.size(); i ) {
IdList.add(filtered.get(i).catID);
}
HashSet<Integer> hashSet = new HashSet<Integer>(IdList);
midList = new ArrayList<Integer>(hashSet) ;
Collections.sort(midList);
Adapter = new CategoryListAdapter(context, R.layout.list1, R.layout.list2, filtered, midList);
List.setAdapter(Adapter);
}
}
}
Комментарии:
1. можете ли вы немного объяснить, как я могу реализовать это решение. Хэнкс