由于某种原因,在显示没有匹配项的空白视图之后,从搜索视图中删除所有文本后,我的列表视图将不会重新出现。
在我的适配器类中,.clear()
中的AFAIK mData.clear();
需要更改为其他内容,但我不知道该怎么做。
ItemListAdapter类
public class ItemListAdapter extends BaseAdapter implements Filterable {
private List<Victoria> mData;
private List<Victoria> mFilteredData;
private LayoutInflater mInflater;
private ItemFilter mFilter;
public ItemListAdapter (List<Victoria> data, Context context) {
mData = data;
mFilteredData = new ArrayList(mData);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mFilteredData.size();
}
@Override
public String getItem(int position) {
return mFilteredData.get(position).getItem();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_row, parent, false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.item_title);
holder.description = (TextView) convertView.findViewById(R.id.item_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(mFilteredData.get(position).getItem());
holder.description.setText(mFilteredData.get(position).getItemDescription());
return convertView;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}
/**
* View holder
*/
static class ViewHolder {
private TextView title;
private TextView description;
}
/**
* Filter for filtering list items
*/
/**
* <p>An array filter constrains the content of the array adapter with
* a prefix. Each item that does not start with the supplied prefix
* is removed from the list.</p>
*/
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (TextUtils.isEmpty(constraint)) {
results.count = mData.size();
results.values = mData;
} else {
//Create a new list to filter on
List<Victoria> resultList = new ArrayList<Victoria>();
for (Victoria str : mData) {
if (str.getItemDescription().toLowerCase().contains(constraint.toString().toLowerCase())) {
resultList.add(str);
}
}
results.count = resultList.size();
results.values = resultList;
}
return results;
}
/**
* Runs on ui thread
* @param constraint the constraint used for the result
* @param results the results to display
*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0) {
//Make list invisible
//Make text view visible
mFilteredData.clear();
notifyDataSetInvalidated();
} else {
mFilteredData = (ArrayList<Victoria>)results.values;
notifyDataSetChanged();
}
}
}
}
过滤之前
用无效字符过滤后
清除搜索视图并尝试再次显示列表后
最佳答案
几个小的修复程序应该使事情适合您。首先,mData
和mFilteredData
数据应始终是两个不同的实例。在构造函数中,让mFilteredData
是从mData
复制的新实例
mFilteredData = new ArrayList(mData);
此外,在
Filter
中,您需要将相应的代码更新为以下内容:if (TextUtils.isEmpty(constraint)) {
results.count = mData.size();
results.values = new ArrayList(mData);
}
然后,您的
getView()
方法应该从mFilteredData
而不是mData
提取。不过,理想情况下,getView()
方法应使用getItem(position)
方法访问数据。这样,如果您更改了从何处访问数据的实现详细信息,则不必记住分别更新getView()
方法。最后,请注意,您的过滤代码仅在适配器不使
mFilteredData
或mData
突变时起作用。通常,由于synchronize
发生在后台线程上,因此必须大量performFiltering()
过滤过程以及适配器的其他部分。如果计划在构造后添加对修改适配器数据的支持,那么您将需要开始添加synchronized
块。