开发者

How do I repopulate my list when deleting constraint of filter

I have been implementing a custom list adapter and custom filter. I have gotten to the point where I can filter out my list by typing, however when I delete my constraints the list does not repopulate. I have used these two sources to get where I am. http://www.google.com/codesearch/p?hl=it#uX1GffpyOZk/core/java/android/widget/ArrayAdapter.java&q=android%20arrayadapter&sa=N&cd=1&ct=rc

and Custom filtering in Android using ArrayAdapter

I am lost as what to do next. This is my code:

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());
            开发者_C百科}
            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();
        }

    }
    //===================================================

}

Do I need to override more methods from Filterable or do I need to do something with my views?

Any help would greatly be appreciated thanks.


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

This is the key to the filter working. Call clone in the constructor with the list passed in and the filter works.

Credit goes to the first answer in this post: How to write a custom filter for ListView with ArrayAdapter


Basically you should be calling notifyDataSetChanged aftet all changes. And you are calling invalidate. This thread seems to have good explanation. Android ListView Adapter notifyDataSetInvalidated() vs notifyDataSetChanged()

Also, I don't think you need notifyDataSetChanged before clear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜