在BaseAdapter -Android中重复getView()位置

时间:2015-10-29 14:34:26

标签: android baseadapter

在我的Android应用程序中,我显示了一个卡列表。我正在使用BaseAdapter。我在ListView上看到所有卡片,但是我遇到了问题。

由于第一张卡片没有显示正确的图像,因此重复显示

getView()。它正在使用第二张卡到最后一张卡的图像。我花了一天时间来解决它。我访问了很多stackoverflow答案,但没有运气。请帮帮我。

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    try {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.wallet_list_item, null);
            holder = new ViewHolder();

            holder.cardIcon = (ImageView) convertView.findViewById(R.id.img_wallet_card);
            holder.cardName = (TextView) convertView.findViewById(R.id.tv_wallet_card_name);
            holder.cardPrice = (TextView) convertView.findViewById(R.id.tv_wallet_card_price);
            holder.cancel  =(ImageView)convertView.findViewById(R.id.cancel);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        WalletRowItem rowItem = (WalletRowItem) getItem(position);

        if(rowItem.getCard_name().equalsIgnoreCase("Kiss")){

           holder.cardIcon.setImageResource(R.drawable.logo);
            holder.cardName.setText("Kiss");
            holder.cardPrice.setText("");
            int bg_red=Integer.parseInt(rowItem.getBackground_red());
            int bg_green=Integer.parseInt(rowItem.getBackground_green());
            int bg_blue=Integer.parseInt(rowItem.getBackground_blue());
            holder.colorbg=Color.rgb(bg_red, bg_green, bg_blue);
            holder.cancel.setVisibility(View.VISIBLE);

            holder.cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewClickListener.onImageClicked(position);
                }
            });
        }else{
                holder.cancel.setVisibility(View.INVISIBLE);
                Picasso.with(context).load(rowItem.getCardImage_url()).error(R.drawable.success_icon).placeholder(R.drawable.plus_icon_actionbar).into(holder.cardIcon);

            holder.cardName.setText(rowItem.getCard_name());

            if(!TextUtils.isEmpty(rowItem.getCard_price())) {
                holder.cardPrice.setText("$" + rowItem.getCard_price());
            }else{
                holder.cardPrice.setText("$" + "0.0");
            }

            int bg_red=Integer.parseInt(rowItem.getBackground_red());
            int bg_green=Integer.parseInt(rowItem.getBackground_green());
            int bg_blue=Integer.parseInt(rowItem.getBackground_blue());
            holder.colorbg=Color.rgb(bg_red, bg_green, bg_blue);
        }

        //Make a shape drawable to make list_item corner rounded .
        ShapeDrawable footerBackground = new ShapeDrawable();
        float[] radii = new float[8];
        radii[0] = 10 ;
        radii[1] = 10 ;
        radii[2] = 10 ;
        radii[3] = 10 ;
        footerBackground.setShape(new RoundRectShape(radii, null, null));
        footerBackground.getPaint().setColor(holder.colorbg);
        convertView.setBackgroundDrawable(footerBackground);

        int red=Integer.parseInt(rowItem.getTitle_red());
        int green=Integer.parseInt(rowItem.getTitle_green());
        int blue=Integer.parseInt(rowItem.getTitle_blue());
        holder.cardPrice.setTextColor(Color.rgb(red, green, blue));
        holder.cardName.setTextColor(Color.rgb(red, green, blue));

    } catch (Exception e) {
        e.getLocalizedMessage();
    }
    return convertView;
}

1 个答案:

答案 0 :(得分:1)

我不确定它是否会解决问题,但我建议始终使用Picasso,包括“吻”的情况。卡,因为它有自己的方式来处理视图回收的图像下载取消(更多细节见https://square.github.io/picasso/

可以通过替换

来完成
holder.cardIcon.setImageResource(R.drawable.logo);

通过

Picasso.load(R.drawable.logo).into(holder.cardIcon);