过滤器列表不显示再次出现空视图后, [英] Filter list not reappearing after empty view shown

查看:122
本文介绍了过滤器列表不显示再次出现空视图后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因被显示为匹配项目空的观点后,我的列表视图将不会从搜索视图中删除所有文字之后重新出现。

For some reason after being shown an empty view for no matching items, my list view won't reappear after removing all text from the search view.

在我的适配器类,AFAIK .clear() mData.clear(); 需要改变别的东西,但我不知道怎么办才好。

Within my adapter class, AFAIK .clear() in mData.clear(); needs to change to something else but I don't know what to.

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();
            }
        }
    }
}

前过滤

Before filtering

有非法字符过滤后

After filtering with an invalid character

清除搜索视图,并试图显示列表中再次之后

After clearing search view and trying to show list again

推荐答案

情侣小的修正应该得到的东西的工作适合你。首先, MDATA mFilteredData 数据应该是在任何时候两个不同的实例。在构造函数中,有 mFilteredData MDATA复制一个新的实例

Couple small fixes should get things working right for you. First, mData and mFilteredData data should be two different instances at all times. In the constructor, have mFilteredData be a new instance copied from mData

mFilteredData = new ArrayList(mData);

此外,在过滤器,你需要更新相应的code到如下:

Additionally in the Filter, you'll need to update the corresponding code to the follow:

if (TextUtils.isEmpty(constraint)) {
    results.count = mData.size();
    results.values = new ArrayList(mData);
}

那么你的 getView()而不是方法应该从 mFilteredData 拉拉MDATA 。理想的情况是虽然, getView()方法应该是使用的getItem(位置)方法来访问数据。这样一来,如果更改其中数据是从访问的实现细节,你不必记住单独升级 getView()方法。

Then your getView() method should be pulling from mFilteredData instead of mData. Ideally though, the getView() method should be using the getItem(position) method to access the data. That way, if you change implementation details of where the data is accessed from, you don't have to remember to update the getView() method separately.

最后,作为一个侧面说明,你的过滤code ++工程,不仅是因为该适配器并没有发生变异 mFilteredData MDATA 。通常情况下,我们必须高度同步过滤过程和适配器的其他部分,因为 performFiltering()在后台线程发生。如果您打算将支持修改后的构建适配器的数据... ...那么你就需要开始添加同步块。

Finally as a side note, your filtering code works only because the adapter does not mutate mFilteredData or mData. Normally, one must heavily synchronize the filtering process and other parts of the adapter because performFiltering() occurs on a background thread. If you plan on adding support for modifying the adapter's data after constructing...then you'll need to start adding synchronized blocks.

这篇关于过滤器列表不显示再次出现空视图后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆