列表视图和搜索视图。过滤问题
Listview With Searchview. Filtering issue
我正在尝试在工具栏中实现搜索视图。所以,到目前为止一切都很顺利。
搜索结果时出现问题。我得到这个例外
Attempt to read from field 'int android.widget.Filter$FilterResults.count' on a null object reference
这是适配器class代码
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;
}
这是文件管理器的代码class
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();
}
}
}
我在上面的这一行得到异常 class if (results.count == 0)//Exception here
有什么办法解决吗?
在 performFiltering
方法中,您 returning null 而不是 results
对象。然后你在 null 上访问 count
属性 会抛出异常。
尝试 return results
而不是 performFiltering
方法中的 null。
我正在尝试在工具栏中实现搜索视图。所以,到目前为止一切都很顺利。
搜索结果时出现问题。我得到这个例外
Attempt to read from field 'int android.widget.Filter$FilterResults.count' on a null object reference
这是适配器class代码
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;
}
这是文件管理器的代码class
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();
}
}
}
我在上面的这一行得到异常 class if (results.count == 0)//Exception here
有什么办法解决吗?
在 performFiltering
方法中,您 returning null 而不是 results
对象。然后你在 null 上访问 count
属性 会抛出异常。
尝试 return results
而不是 performFiltering
方法中的 null。