【发布时间】:2014-03-26 15:36:49
【问题描述】:
我可以过滤掉我的自定义列表。
问题是我的自定义列表视图有 4 个文本字段。 当我搜索时,我得到了结果,但是如果我在列表行的不同字段中有一些重复的文本,那么过滤器会返回相同数量的重复行。
如果说,我的条目是这 2 行数据 {apple, apple, orange, apple},{grapes, melon, mango, peaches} 然后我开始搜索苹果...我将在列表视图中看到 3 行包含重复数据的行,而不是 1 行
我怎样才能停止这种重复?
这是我的代码:
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;
}
【问题讨论】:
标签: android listview search android-edittext