我试图在我的getView()方法中实现视图持有人,但是它崩溃了,并且显示了视图持有人中的所有那些视图为null,这是我的代码。

        if(view==null){
            viewHolder=new ViewHolder();
            view=inflater.inflate(R.layout.find_new_thing_item,null);
            viewHolder.progressBar=(ProgressBar)view.findViewById(R.id.find_new_thing_item_progress_bar);
            viewHolder.mainPictureImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_main_image);
            viewHolder.userIconImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_user_icon);
            viewHolder.thingsNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_name);
            viewHolder.priceTextView=(TextView)view.findViewById(R.id.find_new_thing_item_price);
            viewHolder.userNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_name);
            viewHolder.userCommentTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_comment);
            viewHolder. numberOfLikesTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_likes);
            viewHolder.numberOfAddToDouListTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_add_to_dou_list);
            viewHolder.numberOfReplyTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_reply);
            view.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder)view.getTag();
        }


        new DownloadItemImage().execute(thingsItemArrayList.get(i-1).getMainPicture());
        new DownloadUserIcon().execute(thingsItemArrayList.get(i-1).getUserIcon());
        viewHolder.thingsNameTextView.setText(thingsItemArrayList.get(i - 1).getThingsName());
        viewHolder.priceTextView.setText(thingsItemArrayList.get(i-1).getPrice());
        viewHolder.userNameTextView.setText(thingsItemArrayList.get(i-1).getUserName());
        viewHolder.userCommentTextView.setText(thingsItemArrayList.get(i-1).getUserComment());
        viewHolder.numberOfLikesTextView.setText(thingsItemArrayList.get(i-1).getNumberOfLikes());
        viewHolder.numberOfAddToDouListTextView.setText(thingsItemArrayList.get(i-1).getNumberOfAddToDouList());
        if(thingsItemArrayList.get(i-1).getNumberOfReply().indexOf("评论")>=0){
            viewHolder.numberOfReplyTextView.setText(thingsItemArrayList.get(i-1).getNumberOfReply());
        }else{
            viewHolder.numberOfReplyTextView.setText("评论 0");
        }

        return view;

最佳答案

在您的inflater.inflater()中,您没有指定父视图

做这个:

if(view==null){
    view=inflater.inflate(R.layout.find_new_thing_item,parent,false);
    viewHolder=new ViewHolder(view);
    view.setTag(viewHolder);
}else {
    viewHolder=(ViewHolder)view.getTag();
}
//Set values here.
//viewHolder.thingsNameTextView.setText(....);
//blah blah blah...
return view;


在viewHolder构造函数中:

public viewHolder(View view){
     viewHolder.progressBar=(ProgressBar)view.findViewById(R.id.find_new_thing_item_progress_bar);
        viewHolder.mainPictureImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_main_image);
        viewHolder.userIconImageView=(ImageView)view.findViewById(R.id.find_new_thing_item_user_icon);
        viewHolder.thingsNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_name);
        viewHolder.priceTextView=(TextView)view.findViewById(R.id.find_new_thing_item_price);
        viewHolder.userNameTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_name);
        viewHolder.userCommentTextView=(TextView)view.findViewById(R.id.find_new_thing_item_user_comment);
        viewHolder. numberOfLikesTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_likes);
        viewHolder.numberOfAddToDouListTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_add_to_dou_list);
        viewHolder.numberOfReplyTextView=(TextView)view.findViewById(R.id.find_new_thing_item_number_of_reply);
}

08-16 04:25