0

我正在尝试Gridview用一些TextViews和来填充我的Imageviews。我成功地做到了,但是当我在网格中向下滚动时,图像会改变它们的位置。我已经阅读了与同一问题相关的其他一些问题;喜欢this one question,但它对我没有帮助。

适配器

public class ItemListBaseAdapter extends BaseAdapter {

private static ArrayList<ItemDetails> itemDetailsrrayList;

private LayoutInflater l_Inflater;

private Context context;

// private OnClickListener listner;

public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
    itemDetailsrrayList = results;
    l_Inflater = LayoutInflater.from(context);
    this.context = context;
}

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

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.activity_gridview_items,
                null);
    holder = new ViewHolder();
    convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }       

    holder.txt_itemName = (TextView) convertView
            .findViewById(R.id.tv_item_title);
    holder.txt_itemDescription = (TextView) convertView
            .findViewById(R.id.tv_item_detail);
    holder.txt_itemPrice = (TextView) convertView
            .findViewById(R.id.tv_item_price);
    holder.itemImage = (ImageView) convertView
            .findViewById(R.id.iv_item_image);
    holder.txt_itemName
            .setText(itemDetailsrrayList.get(position).getName());
    holder.txt_itemDescription.setText(itemDetailsrrayList.get(position)
            .getItemDescription());
    holder.txt_itemPrice.setText("Rs "
            + itemDetailsrrayList.get(position).getPrice());
     holder.itemImage.setImageBitmap(itemDetailsrrayList.get(position)
     .getImage());

    return convertView;
}

static class ViewHolder {
    TextView txt_itemName;
    TextView txt_itemDescription;
    TextView txt_itemPrice;
    ImageView itemImage;
    Button likeBtn;
    Button addBtn;
  }
}
4

1 回答 1

-1

你需要打电话findViewByIdconvertView == null

if (convertView == null) {
    convertView = l_Inflater.inflate(R.layout.activity_gridview_items, parent, false);
    holder = new ViewHolder();

    holder.txt_itemName = (TextView) convertView
        .findViewById(R.id.tv_item_title);
    holder.txt_itemDescription = (TextView) convertView
        .findViewById(R.id.tv_item_detail);
    holder.txt_itemPrice = (TextView) convertView
        .findViewById(R.id.tv_item_price);
    holder.itemImage = (ImageView) convertView
        .findViewById(R.id.iv_item_image);

    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}       
于 2013-03-24T12:25:35.463 回答