Listview与Searchview。过滤问题

时间:2016-03-18 07:25:05

标签: android searchview

我正在尝试在工具栏中实现searchview。所以,一切都很顺利。

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

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

中的这一行得到例外

如何解决它?

1 个答案:

答案 0 :(得分:0)

performFiltering方法中,您将返回null而不是results对象。然后你在null上访问count属性,抛出异常。

results方法中尝试返回performFiltering而不是null。