我正在使用listview过滤器,也就是说,有一个编辑框,如果我输入一些关键字,listview数据会相应地改变。首先,有一个数据集。
private ArrayList<PostItem> postList;
假设列表已填充数据,这里是带过滤的适配器:
private class PostAdapter extends ArrayAdapter<PostItem> implements
Filterable {
public PostAdapter(Context context, int resource, List<PostItem> items) {
super(context, resource, 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.post_item, parent, false);
}
PostItem post = getItem(position);
if (post != null) {
TextView title = (TextView) v.findViewById(R.id.postTitle);
TextView date = (TextView) v.findViewById(R.id.postDate);
v.setTag(post.id);
title.setText(Html.fromHtml(post.title));
date.setText(post.name + " "
+ getResources().getString(R.string.post_on) + " "
+ post.date);
}
return v;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
ArrayList<PostItem> filteredList = (ArrayList<PostItem>)results.values;
clear();
for (PostItem item :filteredList) {
add(item);
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<PostItem> filteredArray = new ArrayList<PostItem>();
constraint = constraint.toString().toLowerCase();
for (PostItem item : postList) {
if (item.title.toLowerCase().contains(constraint)) {
filteredArray.add(item);
}
}
results.count = filteredArray.size();
results.values = filteredArray;
return results;
}
};
return filter;
}
}
问题是,clear()函数还会删除postList中包含的所有项目并插入所有最新的过滤结果。怎么解决?我试图复制变量,例如copyPostList = postList
但仍然相同。谢谢你的帮助
更新:更多解释:
首先,postList有项目&#34; abc&#34;;
然后,输入&#34; ab&#34;,列表显示&#34; abc&#34;,这是正确的
之后,键入&#34; xyz&#34;,列表显示任何内容,这是正确的
最后,键入&#34; abc&#34;,列表显示任何内容,这是不正确的
答案 0 :(得分:3)
public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> fullList;
private ArrayList<String> mOriginalValues;
private ArrayFilter mFilter;
public AutoCompleteAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
fullList = (ArrayList<String>) objects;
mOriginalValues = new ArrayList<String>(fullList);
}
@Override
public void add(String object) {
super.add(object);
fullList.add(object);
mOriginalValues.add(object);
}
@Override
public void clear() {
super.clear();
fullList.clear();
mOriginalValues.clear();
}
@Override
public int getCount() {
return fullList.size();
}
@Override
public String getItem(int position) {
return fullList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setEllipsize(TextUtils.TruncateAt.START);
return view;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<String>(fullList);
}
ArrayList<String> list = new ArrayList<String>(mOriginalValues);
results.values = list;
results.count = list.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values != null) {
fullList = (ArrayList<String>) results.values;
} else {
fullList = new ArrayList<String>();
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
检查出来。这个适配器效果很好。祝你好运。
答案 1 :(得分:0)
最终代码,为我的案例添加过滤和getView
private class PostAdapter extends ArrayAdapter<PostItem> implements Filterable {
private ArrayList<PostItem> fullList;
private ArrayList<PostItem> mOriginalValues;
private ArrayFilter mFilter;
public PostAdapter(Context context, int resource, List<PostItem> objects) {
super(context, resource, objects);
fullList = (ArrayList<PostItem>) objects;
mOriginalValues = new ArrayList<PostItem>(fullList);
}
@Override
public void add(PostItem object) {
super.add(object);
fullList.add(object);
mOriginalValues.add(object);
}
@Override
public void clear() {
super.clear();
fullList.clear();
mOriginalValues.clear();
}
@Override
public int getCount() {
return fullList.size();
}
@Override
public PostItem getItem(int position) {
return fullList.get(position);
}
@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.post_item, parent, false);
}
PostItem post = getItem(position);
if (post != null) {
TextView title = (TextView) v.findViewById(R.id.postTitle);
TextView date = (TextView) v.findViewById(R.id.postDate);
v.setTag(post.id);
title.setText(Html.fromHtml(post.title));
date.setText(post.name + " "
+ getResources().getString(R.string.post_on) + " "
+ post.date);
}
return v;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
ArrayList<PostItem> list = new ArrayList<PostItem>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<PostItem>(fullList);
}
for (PostItem item : mOriginalValues) {
if (item.title.toLowerCase().contains(prefix.toString().toLowerCase())) {
list.add(item);
}
}
results.values = list;
results.count = list.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values != null) {
fullList = (ArrayList<PostItem>) results.values;
} else {
fullList = new ArrayList<PostItem>();
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
感谢您的帮助