自定义扩展列表视图子搜索过滤器 [英] Custom expandable list view with child search filter
问题描述
我已经实现了一个列表视图搜索过滤器,但现在,我必须把它改为扩展列表视图的孩子的搜索过滤器。会有一个编辑的文本作为一个搜索栏,并过滤所有组的所有孩子。这是该方案,
*人 - 对象包括姓名,地址,电话号码和照片
1组 - 友(儿童1 - 人,儿童2 - 人,儿童3 - 人)
2组 - 家庭(儿童1 - 人,儿童2 - 人)
3组 - Officemates(儿童1 - 人,儿童2 - 人,儿童3 - 人,儿童4 - 人)
截至目前,我得从口阵列适配器立足扩展列表适配器和过滤的。有人可以帮助我?谢谢你。
编辑=(EditText上)findViewById(R.id.editText1);
edit.addTextChangedListener(filterTextWatcher);
私人TextWatcher filterTextWatcher =新TextWatcher(){
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){
}
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
}
公共无效afterTextChanged(编辑S){
。((可筛选)((ListAdapter)适配器))用getFilter()过滤器(edit.getText()的toString());
}
};
公共类ListAdapter扩展BaseExpandableListAdapter实现过滤的{
公共无效notifyDataSetInvalidated(){
super.notifyDataSetInvalidated();
}
公共过滤用getFilter(){
如果(过滤== NULL)
过滤器=新MangaNameFilter();
返回过滤器;
}
私有类MangaNameFilter扩展过滤器{
@覆盖
保护FilterResults performFiltering(CharSequence的约束){
//注:此功能是*总*从后台线程调用,
//没有UI线程。
约束= edit.getText()的toString()与toLowerCase()。
FilterResults导致=新FilterResults();
如果(约束= NULL和放大器;!&安培; constraint.toString()长度()0){
detailsList = detailsSer.GetAlldetails();
dupCatList = detailsList;
ArrayList的< detailsEntity> FILT =新的ArrayList< detailsEntity>();
ArrayList的< detailsEntity> lItems =新的ArrayList< detailsEntity>();
同步(本){
lItems.addAll(dupCatList);
}
的for(int i = 0,L = lItems.size(); I<升;我++){
detailsEntity米= lItems.get(ⅰ);
如果(m.description.toLowerCase()。包括(约束))
filt.add(米);
}
result.count = filt.size();
result.values = FILT;
} 其他 {
detailsList = detailsSer.GetAlldetails();
dupCatList = detailsList;
同步(本){
result.count = dupCatList.size();
result.values = dupCatList;
}
}
返回结果;
}
@燮pressWarnings(未登记)
@覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果){
//注:此功能*总*从UI线程调用。
过滤=(ArrayList的< detailsEntity>)result.values;
ArrayList的<整数GT; IDLIST =新的ArrayList<整数GT;();
IdList.clear();
的for(int i = 0; I< filtered.size();我++){
IdList.add(filtered.get㈠.catID);
}
HashSet的<整数GT; HashSet的=新的HashSet<整数GT;(IDLIST);
midList =新的ArrayList<整数GT;(HashSet的);
Collections.sort(midList);
适配器=新CategoryListAdapter(背景下,R.layout.list1,R.layout.list2,过滤,midList);
List.setAdapter(适配器);
}
}
}
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 - object consist of name, address, phone number and photo.
Group 1 - Friends (Child 1 - Person, Child 2 - Person, Child 3 - Person)
Group 2 - Family (Child 1 - Person, Child 2 - Person)
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屋!