gridview中的图像改变了他们在android中滚动的位置

时间:2013-03-24 12:10:24

标签: android gridview adapter

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

Aadapter

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;
  }
}

1 个答案:

答案 0 :(得分:-1)

您需要在findViewById时致电convertView == 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();
}