public class SpotAdapter extends ArrayAdapter<Spot> {
private ArrayList<Spot> mFilteredList, mUnfilteredList;
private Filter mFilter;
public SpotAdapter(Context context, int resource, List<Spot> items) {
super(context, resource, items);
mUnfilteredList = (ArrayList<Spot>) items;
mFilteredList = (ArrayList<Spot>) items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.record_row, null);
}
Spot p = (Spot) mFilteredList.get(position);
if (p != null) {
TextView nameView = (TextView) v.findViewById(R.id.name);
TextView addressView = (TextView) v.findViewById(R.id.address);
TextView typeView = (TextView) v.findViewById(R.id.type);
TextView contactView = (TextView) v.findViewById(R.id.contact);
ImageView starBtn = (ImageView) v.findViewById(R.id.star);
contactView.setVisibility(View.GONE);
nameView.setText(p.name);
addressView.setText(p.address);
typeView.setText(p.type == 1 ? "Leisure" : "Shopping");
starBtn.setTag("" + p.id);
starBtn.setVisibility(p.isStar ? View.VISIBLE : View.GONE);
}
return v;
}
@Override
public Filter getFilter() {
if(mFilter == null) {
mFilter = new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilteredList = (ArrayList<Spot>)results.values;
clear();
int count = mFilteredList.size();
for (int i=0; i<count; i++)
{
Spot s = (Spot)mFilteredList.get(i);
add(s);
}
//
// mFilteredList = (ArrayList<Spot>)results.values;
// notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d("test1","c "+constraint);
Log.d("test1","s "+mUnfilteredList.size());
FilterResults results = new FilterResults();
ArrayList<Spot> newList = new ArrayList<Spot>();
for (Spot obj : mUnfilteredList) {
// Compare the custom object and add to list if match
if (constraint.equals("bookmark")) {
if (obj.isStar == true)
newList.add(obj);
} else if (constraint.equals("select all")) {
newList.add(obj);
}
}
Log.d("test1",""+newList.size());
results.values = newList;
results.count = newList.size();
return results;
}
};
}
return mFilter;
}
}
以上是自定义过滤器,我的想法是有两个过滤选项&#34; bookMark&#34; /&#34;选择全部&#34;,这意味着选择所有显示原始列表,而bookMark仅显示书签项目。
问题是,如果我点击书签,然后点击全选,结果仍然是书签项目。
所以我记录结果,我发现第二次mUnfilteredList = (ArrayList<Spot>) items;
会重新运行,所以未过滤的列表只是最后一个过滤结果,而不是保留原始列表。如何更改以解决问题?
感谢您的帮助
答案 0 :(得分:0)
mFilteredList = new ArrayList(items);