可过滤的listview在baseadapter上返回null

时间:2014-04-08 15:35:35

标签: java android listview filter baseadapter

任何人都可以看到导致logcat返回null的原因,因为它没有告诉我任何有用的东西。它一直工作,直到我尝试添加一个过滤器,现在它崩溃,甚至在列表视图上显示数据。

public class PostDetailsAdapter extends BaseAdapter implements Filterable {
private static List<PostDetails> PostsArray;
List<PostDetails> mOriginalValues;

private LayoutInflater mInflater;

public PostDetailsAdapter(Context context, List<PostDetails> results) {
    PostsArray = results;
    mInflater = LayoutInflater.from(context);
}

public int getCount() {
    return PostsArray.size();
}

public Object getItem(int position) {
    return PostsArray.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.post_row, null);

        holder = new ViewHolder();
        holder.Title = (TextView) convertView.findViewById(R.id.title);
        holder.Date = (TextView) convertView.findViewById(R.id.date);
        holder.Description = (TextView) convertView.findViewById(R.id.description);
        holder.Author = (TextView) convertView.findViewById(R.id.author);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.Title.setText(PostsArray.get(position).getTitle());
    holder.Date.setText(PostsArray.get(position).getDate());
    holder.Description.setText(PostsArray.get(position).getDescription());
    holder.Author.setText(PostsArray.get(position).getAuthor());

    holder.Title.setTextColor(Color.BLACK);
    holder.Date.setTextColor(Color.BLACK);
    holder.Description.setTextColor(Color.BLACK);
    holder.Author.setTextColor(Color.BLACK);

    return convertView;
}

static class ViewHolder {
    TextView Title;
    TextView Date;
    TextView Description;
    TextView Author;
}

@Override
public Filter getFilter() 
{
    Filter filter = new Filter() 
    {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) 
        {

            PostsArray = (List<PostDetails>) results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) 
        {
            FilterResults results = new FilterResults();
            List<PostDetails> FilteredArrList = new ArrayList<PostDetails>();

            if (mOriginalValues == null) 
            {
                System.out.println("");
                mOriginalValues = new ArrayList<PostDetails>(PostsArray);
            }

            if (constraint == null || constraint.length() == 0) 
            {

                // set the Original result to return  
                results.count = mOriginalValues.size();
                results.values = mOriginalValues;
            } 
            else 
            {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mOriginalValues.size(); i++) 
                {
                    PostDetails data = mOriginalValues.get(i);
                    if (data.getTitle().toLowerCase().contains(constraint.toString())) 
                    {
                        FilteredArrList.add(data);
                    }
                }
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }
            return results;
        }
    };
    return filter;
}
}

1 个答案:

答案 0 :(得分:0)

事实证明代码没有任何问题,我在完全设置列表视图之前尝试将过滤器应用于此列表视图。不完全理解AsyncTask的乐趣:)