Karey Powell
Karey Powell

Reputation: 482

Filter a Listview using a spinner in android

I have this Listview that contains both text and images. Each list item contains four key elements. I want to be able to filter the listview using either of the three TextViews by an option selected from the spinner. This is what I have done so far:

    public class LazyVenueAdapter extends BaseAdapter implements Filterable {

        private Activity activity;
        private ArrayList<HashMap<String, String>> data;
        private static LayoutInflater inflater = null;
        public ImageLoader imageLoader;

        public LazyVenueAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
            activity = a;
            data = d;
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader = new ImageLoader(activity.getApplicationContext());
        }

        public int getCount() {
             return data.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View vi = convertView;
            if (convertView == null)
                vi = inflater.inflate(R.layout.venue_list_item, null);

            TextView title = (TextView) vi.findViewById(R.id.venueName);
            TextView location = (TextView) vi.findViewById(R.id.venueLocation);
            TextView tags = (TextView) vi.findViewById(R.id.venueTags);
            ImageView thumb_image = (ImageView) vi.findViewById(R.id.venueImage);

            HashMap<String, String> venue = new HashMap<String, String>();
            venue = data.get(position);

            // Setting all values in listview
            title.setText(venue.get(VenuesFragment.KEY_TITLE));
            location.setText(venue.get(VenuesFragment.KEY_LOCATION));
            tags.setText(venue.get(VenuesFragment.KEY_TAGS));
            imageLoader.DisplayImage(venue.get(VenuesFragment.KEY_THUMB_URL),
                    thumb_image);

            return vi;
        }

        @Override
        public Filter getFilter() {

            Filter filter = new Filter() {

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {

                    FilterResults results = new FilterResults();
                    ArrayList<HashMap<String, String>> filteredArrayVenues = new ArrayList<HashMap<String, String>>();
                    data.clear();

                    if (constraint == null || constraint.length() == 0) {
                        results.count = data.size();
                        results.values = data;
                    } else {
                        constraint = constraint.toString();
                        for (int index = 0; index < data.size(); index++) {
                            HashMap<String, String> dataVenues = data.get(index);

                            if (dataVenues.get(VenuesFragment.KEY_TAGS).toString().startsWith(
                            constraint.toString())) {
                                filteredArrayVenues.add(dataVenues);
                            }
                        }

                        results.count = filteredArrayVenues.size();
                        System.out.println(results.count);

                        results.values = filteredArrayVenues;
                        Log.e("VALUES", results.values.toString());
                    }

                    return results;
                }

                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint,
                FilterResults results) {

                    data = (ArrayList<HashMap<String, String>>) results.values;
                    notifyDataSetChanged();
                }

            };

            return filter;
        }
    }

The problem that I am having is that when I select an option from the drop down list I get back an empty result set.

Upvotes: 1

Views: 4514

Answers (2)

Sam
Sam

Reputation: 86948

You don't receive any results because you delete the information that you are trying to build your results from with data.clear();. If data is now empty (ie data.size() == 0) then this loop will never execute:

for (int index = 0; index < data.size(); index++) {

Removing this line might solve your problem:

data.clear();

Upvotes: 1

Tai Tran
Tai Tran

Reputation: 1404

You have to filter your ArrayList<HashMap<String, String>> data first, then call notifyDataSetChanged() in your adapter

Upvotes: 0

Related Questions