带有子搜索过滤器的自定义可扩展列表视图 [英] Custom expandable list view with child search filter
问题描述
我已经实现了带有搜索过滤器的列表视图,但现在,我必须将其更改为带有子搜索过滤器的可扩展列表视图.将有一个编辑文本作为搜索栏并过滤所有组的所有子项.这是场景,
I've already implemented a list view with search filter but right now, I have to changed it to expandable list view with child search filter. There would be an edit text as a search bar and filters all the child of all groups. This is the scenario,
*Person - 对象由姓名、地址、电话号码和照片组成.
*Person - object consist of name, address, phone number and photo.
第 1 组 - 朋友(儿童 1 - 人,儿童 2 - 人,儿童 3 - 人)
Group 1 - Friends (Child 1 - Person, Child 2 - Person, Child 3 - Person)
第 2 组 - 家庭(儿童 1 - 人,儿童 2 - 人)
Group 2 - Family (Child 1 - Person, Child 2 - Person)
第 3 组 - 办公室人员(儿童 1 - 人,儿童 2 - 人,儿童 3 - 人,儿童 4 - 人)
Group 3 - Officemates (Child 1 - Person, Child 2 - Person, Child 3 - Person, Child 4 - Person)
到目前为止,我必须从数组适配器移植到基本的可扩展列表适配器和可过滤的.有人可以帮我弄这个吗?谢谢.
As of now, I have to port from array adapter to base expandable list adapter and filterable. Can someone help me with this? Thanks.
推荐答案
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 && 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 = filt;
} else {
detailsList = detailsSer.GetAlldetails();
dupCatList = detailsList;
synchronized(this) {
result.count = dupCatList.size();
result.values = dupCatList;
}
}
return result;
}
@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);
}
}
}
这篇关于带有子搜索过滤器的自定义可扩展列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!