我可以筛选出我的自定义列表。使用CustomView上的编辑文本进行搜索功能ListView
问题是我的自定义listview有4个文本字段。 当我搜索我得到的结果,但如果我有我的列表行中的不同领域的一些重复的文本,然后过滤器返回相同数量的重复行。
如果说,我的条目是这些2行数据{苹果,苹果,橙,苹果},{葡萄,瓜,芒果,桃子} ,我开始搜索苹果...我会看到3行重复的数据在我的列表视图而不是一个
我该如何阻止这种重复?
这里是我的代码:
adapter = new MyAdapter(
this,
list,
R.layout.list_row,
new String[] {fruit1, fruit2, fruit3, fruit4 },
new int[] {R.id.fruit1, R.id.fruit2, R.id.fruit3,R.id.fruit4});
populateList();
setListAdapter(adapter);
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
ListScreen.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void afterTextChanged(Editable arg0) {}
});
class MyAdapter extends SimpleAdapter{
public PassAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
arrow = (ImageView) view.findViewById(R.id.arrow);
data= (LinearLayout) view.findViewById(R.id.data);
arrow.setImageResource(R.drawable.arrow_down);
data.setVisibility(View.GONE);
return view;
}
这里是我的自定义过滤器的代码以及 但这并不刷新我的名单
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults res = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
res.values = tempList;
res.count = tempList.size();
} else {
synchronized(this){
// We perform filtering operation
List<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
for (HashMap<String, String> data : tempList) {
if (data.get("fruit1").toUpperCase().startsWith(constraint.toString().toUpperCase()))
dataList.add(data);
}
res.values = dataList;
res.count = dataList.size();
}
}
return res;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0)
notifyDataSetInvalidated();
else {
tempList = (ArrayList<HashMap<String, String>>) results.values;
notifyDataSetChanged();
}
}
};
return filter;
}
您可能要贴上'MyAdapter'代码。 – curtisLoew
添加了适配器代码:) –
我建议你自己编写自定义的[Filter](默认实现显然不适合你的需求)。 – curtisLoew