删除筛选器的约束时,如何重新填充列表



我一直在实现一个自定义列表适配器和自定义过滤器。我已经到了可以通过键入筛选出列表的地步,但是当我删除约束时,列表不会重新填充。我使用了这两个来源来获得我现在的位置。http://www.google.com/codesearch/p?hl=it#uX1GffpyOZk/core/java/android/widget/ArrayAdapter.java&q=android%20arrayadapter&sa=N&cd=1&ct=rc

和使用ArrayAdapter 在Android中进行自定义过滤

我不知道下一步该怎么办。这是我的代码:

private class stationAdapter extends ArrayAdapter<Station>
{
    //======================================
    public ArrayList<Station> stations;
    public ArrayList<Station> filtered;
    private Filter filter;
    //=====================
    public stationAdapter(Context context, int textViewResourceId, ArrayList<Station> stations)
    {
        super(context, textViewResourceId, stations);
        this.filtered = stations;
        this.stations = filtered;
        this.filter = new StationFilter();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = convertView;
        if (v == null)
        {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Station temp = stations.get(position);
        if (temp != null)
        {
            TextView stationName = (TextView) v.findViewById(R.id.stationname);
            TextView serviced = (TextView) v.findViewById(R.id.inservice);
            try
            {
                if (temp.isRedLine())
                {
                    // v.setBackgroundResource(R.color.red);
                    ImageView imageView = (ImageView) v.findViewById(R.id.icon);
                    imageView.setImageResource(R.drawable.redstation);
                    v.setBackgroundResource(temp.getAreaColour());
                }
                else
                {
                    ImageView imageView = (ImageView) v.findViewById(R.id.icon);
                    imageView.setImageResource(R.drawable.greenstation);
                    v.setBackgroundResource(temp.getAreaColour());
                }
            }
            catch (Exception e)
            {
                Log.d(TAG, "Null pointer");
            }
            if (stationName != null)
            {
                stationName.setText(temp.getName());
            }
            if (serviced != null)
            {
                serviced.setText(temp.getIrishName());
            }
        }
        return v;
    }
    //=====================================
    @Override
    public Filter getFilter()
    {
        if(filter == null)
            filter = new StationFilter();
        return filter;
    }
    private class StationFilter extends Filter
    {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // NOTE: this function is *always* called from a background thread, and
            // not the UI thread.
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if(constraint != null && constraint.toString().length() > 0)
            {
                ArrayList<Station> filt = new ArrayList<Station>();
                ArrayList<Station> lItems = new ArrayList<Station>();
                synchronized (this)
                {
                    lItems.addAll(stations);
                }
                for(int i = 0, l = lItems.size(); i < l; i++)
                {
                    Station m = lItems.get(i);
                    if(m.getName().toLowerCase().startsWith((String) constraint))
                    {
                        filt.add(m);
                    }
                }
                result.count = filt.size();
                result.values = filt;
            }
            else
            {
                synchronized(this)
                {
                    result.values = stations;
                    result.count = stations.size();
                }
            }
            return result;
        }
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // NOTE: this function is *always* called from the UI thread.
            filtered = (ArrayList<Station>)results.values;
            notifyDataSetChanged();
            clear();
            for(int i = 0, l = filtered.size(); i < l; i++){
                add(filtered.get(i));
            }
            notifyDataSetInvalidated();
        }
    }
    //===================================================
}

我需要覆盖Filterable中的更多方法吗?还是需要对我的视图做些什么?

如有任何帮助,我们将不胜感激。

        protected void cloneItems(ArrayList<Station> items) {
        for (Iterator<Station> iterator = items.iterator(); iterator
        .hasNext();) {
            Station s = (Station) iterator.next();
            originalItems.add(s);
        }
    }

这是过滤器工作的关键。使用传入的列表在构造函数中调用clone,筛选器即可工作。

这篇文章的第一个答案是:如何使用ArrayAdapter 为ListView编写自定义过滤器

基本上,您应该在所有更改之后调用notifyDataSetChanged。而您正在调用invalid。这条线索似乎有很好的解释。Android ListView适配器notifyDataSetInvalidated()vs notifyDataSetChanged()

此外,我认为您不需要在clear之前通知DataSetChanged。

最新更新