我正在开发一个项目,其中有一个带有imageview的自定义列表视图和一个位于imageview顶部的textview。我已经完成了以下过滤代码。
主要片段:
CategoriesAdapter adapter;
edt_SearchBar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
CategoriesAdapter类:
public class CategoriesAdapter extends BaseAdapter {
List<CategoryDataModel> category_list,category_filter_list;
Context context;
LayoutInflater inflater;
ImageLoader imageLoader;
ViewHolder vh;
ValueFilter filter;
int counter=0;
String[] images_array;
public CategoriesAdapter(List<CategoryDataModel> category_list, Context context) {
this.category_list=category_list;
this.category_filter_list=category_list;
this.context=context;
inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader= AppController.getInstance().getImageLoader();
images_array=new String[]{"kochi_cinepolis.png","kochi_padma.png"};
}
@Override
public int getCount() {
return category_list.size();
}
@Override
public Object getItem(int position) {
return category_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView=inflater.inflate(R.layout.category_view_style, null);
vh=new ViewHolder();
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
vh.imageView = (NetworkImageView) convertView .findViewById(R.id.imageView_grid);
vh.categoryName= (TextView) convertView.findViewById(R.id.tvcategory);
convertView.setTag(vh);
}
else
{
vh=(ViewHolder) convertView.getTag();
}
vh.imageView.setImageUrl(AppConstant.IMAGE_FOLDER +images_array[position], imageLoader);
vh.imageView.setDefaultImageResId(R.drawable.default_category_loading);
vh.categoryName.setText(category_list.get(position).getCategory_name().toUpperCase());
return convertView;
}
public class ViewHolder
{
NetworkImageView imageView;
TextView categoryName;
}
public ValueFilter getFilter() {
if(filter==null) {
filter=new ValueFilter();
}
return filter;
}
public class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results=new FilterResults();
if(constraint!=null && constraint.length()>0){
String cons_value=constraint.toString();
List<CategoryDataModel> filterList=new ArrayList<>();
for(int i=0;i<category_filter_list.size();i++){
if(category_filter_list.get(i).getCategory_name().toLowerCase().contains(cons_value.toLowerCase())) {
filterList.add(category_filter_list.get(i));
}
}
results.count=filterList.size();
results.values=filterList;
}else{
results.count=category_filter_list.size();
results.values=category_filter_list;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
category_list=(List<CategoryDataModel>) results.values;
notifyDataSetChanged();
}
}
}
问题如下。现在我在listview中有两个项目。我正在基于textview中的文本搜索列表视图。但是当返回搜索结果时,列表视图行的相应图像显示错误。第一行的图像是显示的而不是正确的行。请尽早帮助。
答案 0 :(得分:0)
您的适配器需要实现Filterable接口,它必须实现getFilter(),并且您需要覆盖自定义适配器中的getFilter()并相应地过滤数据源并通知列表视图数据集已更改。