gpt4 book ai didi

android - 如何使用 ListView 为自定义 BaseAdapter 实现过滤器?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:47 26 4
gpt4 key购买 nike

这是我从 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;
}
};
}
}

我搜索了一下,发现我需要实现的两个功能是:

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

我的过滤约束是“name”字符串数组,我想 performFiltering 会根据文本“constraint”执行搜索。让我困惑的是results.value的类型是object。那么在我对 name 执行搜索后,我应该将什么类型的数据分配给 results.valuespublishResults 函数有什么作用?谁能给我一个关于这两个功能的例子?提前致谢,

最佳答案

您可以在 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;
}

关于android - 如何使用 ListView 为自定义 BaseAdapter 实现过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8581604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com