这是我从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;
}
};
}
}
我搜索并发现我需要实现的两个功能是:
protected void publishResults(CharSequence constraint, FilterResults results)
protected FilterResults performFiltering(CharSequence constraint)
我的过滤器约束是“名称”字符串数组,我猜performFiltering
将根据文本“约束”执行搜索。困扰我的是results.value
的类型是object
。因此,在name
上执行搜索后,我应该将哪种类型的数据分配给results.values
?函数publishResults
有什么作用?有谁可以给我一个关于这两个功能的例子?提前谢谢,
答案 0 :(得分:2)
您可以在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;
}