Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement filter for custom BaseAdapter with ListView?

This is my custom adapter inherited from BaseAdapter:

public class LocationItemAdapter extends BaseAdapter implements Filterable {
    private Activity context;
    private String[] names;
    private Bitmap[] iconBitmaps;
    private String[] categories;
    private String[] ratings;
    private boolean notifyChanged = true;

    public LocationItemAdapter(Activity activityContext, String[] names, Bitmap[] iconBitmaps, String[] categories, String[] ratings) {
        super();
        this.context = activityContext;
        this.names = names;
        this.iconBitmaps = iconBitmaps;
        this.categories = categories;
        this.ratings = ratings;
    }

    public int getCount() {
        return names.length;
    }

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

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

    public static class ItemViewHolder {
        TextView nameTextView;
        TextView categoryTextView;
        TextView ratingTextView;
        ImageView mapIconImageView;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.location_item, null);
            holder = new ItemViewHolder();
            holder.nameTextView = (TextView) convertView.findViewById(R.id.location_item_xml_textview_name);
            holder.categoryTextView = (TextView) convertView.findViewById(R.id.location_item_xml_textview_category);
            holder.ratingTextView = (TextView) convertView.findViewById(R.id.location_item_xml_textview_rating);
            holder.mapIconImageView = (ImageView) convertView.findViewById(R.id.location_item_xml_imageview_location_icon);

            convertView.setTag(holder);
        }
        else {
            holder = (ItemViewHolder) convertView.getTag();
        }

        holder.nameTextView.setText(names[position]);
        holder.categoryTextView.setText(categories[position]);
        holder.ratingTextView.setText(ratings[position]);
        holder.mapIconImageView.setImageBitmap(iconBitmaps[position]);

        return convertView;
    }

    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        notifyChanged = true;
    }

    public android.widget.Filter getFilter() {
        return new android.widget.Filter() {
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                if (constraint != null && constraint.toString() > 0) {

                }
                else {
                }
                results.values = filteredResults;
                return results;
            }
        };
    }
}

I searched and found that the two functions that I need to implement are:

  1. protected void publishResults(CharSequence constraint, FilterResults results)
  2. protected FilterResults performFiltering(CharSequence constraint)

My filter constraint is the "name" string array, and I guess performFiltering would perform a search based on the text "constraint". What confused me is that the type of results.value is object. So after I perform a search on name, what type of data should I assign to results.values? And what does the function publishResults do? Could anyone give me an example on these two functions? Thanks in advance,

like image 207
Chan Avatar asked Dec 20 '11 20:12

Chan


1 Answers

You could do the following in the performFiltering

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

              names = results.values();  // copy the results to your original names and refresh list
              notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            String [] resultNames=new String[names.length];
            int count=0;

            if (constraint != null && constraint.toString() > 0) {
                  for(String name:names)
                  {
                       if(name.contains(constraint))  // whatever search condition you need
                           resultNames[count++]=name;
                  }
            }
            else {

            }
            results.values = resultNames;
            return results;
        }
like image 143
blganesh101 Avatar answered Nov 01 '22 17:11

blganesh101