当一个条目布局中imageview的个数不确定,特别是个数为零时,需要把这部分空间释放出去,这个时候如果动态创建imageview,由于启用了listview的复用机制,会导致imageview被重复创建,所以当利用了listview的复用机制时,在adapter不能new对象。这时候我们需要在条目布局中首先创建imageview,在adapter中有选择的显示和隐藏。Java代码如下:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder vh;
if(convertView==null){
convertView=inflater.inflate(R.layout.item_businessdetail_layout, parent,false);
vh=new ViewHolder();
vh.ivAvatar=(CircleImageView) convertView.findViewById(R.id.iv_detail_avatar);
vh.ivRating=(ImageView) convertView.findViewById(R.id.iv_detail_rating);
vh.ivPic1=(ImageView) convertView.findViewById(R.id.iv_detail_pic1);
vh.ivPic2=(ImageView) convertView.findViewById(R.id.iv_detail_pic2);
vh.ivPic3=(ImageView) convertView.findViewById(R.id.iv_detail_pic3);
vh.tvAvgprice=(TextView) convertView.findViewById(R.id.tv_detail_avgprice);
vh.tvUsername=(TextView) convertView.findViewById(R.id.tv_detail_username);
vh.tvContent=(TextView) convertView.findViewById(R.id.tv_detail_content);
convertView.setTag(vh);
}else{
vh=(ViewHolder) convertView.getTag();
}
Comment comment=getItem(position);
vh.tvUsername.setText(comment.getUsername());
vh.tvContent.setText(comment.getContent());
vh.tvAvgprice.setText("¥"+comment.getAvgprice()+"/人");