1

我有这个包含文本和图像的 Listview。每个列表项包含四个关键元素。我希望能够通过从微调器中选择的选项使用三个 TextView 中的任何一个来过滤列表视图。这是我到目前为止所做的:

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

我遇到的问题是,当我从下拉列表中选择一个选项时,我得到一个空的结果集。

4

2 回答 2

1

您没有收到任何结果,因为您删除了尝试从中构建结果的信息data.clear();。如果data现在为空(即data.size() == 0),则此循环将永远不会执行:

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

删除此行可能会解决您的问题:

data.clear();
于 2012-06-11T18:14:38.420 回答
0

你必须先过滤你的ArrayList<HashMap<String, String>> data,然后调用notifyDataSetChanged()你的adapter

于 2012-06-11T00:30:47.813 回答