我可以篩選出我的自定義列表。使用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