具有搜索视图的列表视图.过滤问题



>我正在尝试在工具栏中实现搜索视图。所以,到目前为止一切都很顺利。

当我搜索结果时出现问题。我得到这个例外

Attempt to read from field 'int android.widget.Filter$FilterResults.count' on a null object reference

这是适配器类代码

public class CustomListAdapter extends BaseAdapter implements Filterable {
Context context;
MainDTO values;
LayoutInflater inflater;
List<PostsDTO> list;
CustomFilter customFilter;
public CustomListAdapter(Context context, MainDTO values) {
    this.context = context;
    this.values = values;
    list=values.getPosts();
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    return values.getPosts().size();
}
@Override
public Object getItem(int position) {
    return position;
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    final ImageLoader imageLoader = ImageLoader.getInstance();
    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = inflater.inflate(R.layout.item_list, parent, false);
        viewHolder.category = (TextView) convertView.findViewById(R.id.category);
        viewHolder.title = (TextView) convertView.findViewById(R.id.article_title);
        viewHolder.date = (TextView) convertView.findViewById(R.id.date);
        viewHolder.comment = (TextView) convertView.findViewById(R.id.comment);
        viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
        viewHolder.imgcoment = (ImageView) convertView.findViewById(R.id.imgcomment);
        viewHolder.author_pic = (RoundedImageView) convertView.findViewById(R.id.author_pic);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    if (!values.getPosts().get(position).getCategories().isEmpty()) {
        viewHolder.category.setText(list.get(position).getCategories().get(0).getTitle());
    }
    viewHolder.title.setText(Html.fromHtml(list.get(position).getTitle()));
    viewHolder.date.setText(list.get(position).getDate());
    viewHolder.comment.setText("" +list.get(position).getCommentCount());
    if (list.get(position).getThumbnail_images() != null) {
        imageLoader.displayImage(list.get(position).getThumbnail_images().getFull().getUrl(), viewHolder.imageView);
    }
    final String hash = GravatarMD5.md5Hex(list.get(position).getAuthor().getEmail());
    imageLoader.displayImage(Constants.URL_BASE_GRAVATAR + hash, viewHolder.author_pic);
    viewHolder.imgcoment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, DisqusCommentsActivity.class);
            intent.putExtra("postUrl", list.get(position).getUrl());
            intent.putExtra("title", list.get(position).getTitle());
            context.startActivity(intent);
        }
    });
    viewHolder.author_pic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ProfileActivity.class);
            intent.putExtra("hash", hash);
            intent.putExtra("authorName", list.get(position).getAuthor().getName());
            intent.putExtra("authorId", list.get(position).getAuthor().getId());
            context.startActivity(intent);

        }
    });
    return convertView;
}
@Override
public Filter getFilter() {
    if (customFilter == null)
        customFilter = new CustomFilter();
    return customFilter;
}
private static class ViewHolder {
    public TextView category;
    public TextView title;
    public TextView date;
    public TextView comment;
    public ImageView imageView;
    public ImageView imgcoment;
    public RoundedImageView author_pic;
}

下面是文件管理器类的代码

public class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            // We implement here the filter logic
            if (constraint == null || constraint.length() == 0) {
                results.values = values.getPosts();
                results.count = values.getPosts().size();
            } else {
                List<PostsDTO> postsDTOs = new ArrayList<PostsDTO>();
                for (PostsDTO p : values.getPosts()) {
                    if (p.getTitle().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                        postsDTOs.add(p);
                }
                results.values = values.getPosts();
                results.count = values.getPosts().size();
            }
            return null;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count == 0)//Exception here
                notifyDataSetInvalidated();
            else {
                list = (List<PostsDTO>) results.values;
                notifyDataSetChanged();
            }
        }
    }

我在上面的类if (results.count == 0)//Exception here的这一行得到异常

如何解决它的任何想法?

performFiltering方法中,您返回的是 null 而不是results对象。然后,您将访问 null 上的count属性,该属性会引发异常。

尝试在方法

中返回 results 而不是 null performFiltering

相关内容

  • 没有找到相关文章

最新更新