2

In my Android Application I am showing a list of Cards. I am using BaseAdapter. All cards are visible to me on the ListView but I am getting an issue.

getView() is called with repeated positions due to the first card not showing the correct image. It is using the image from the 2nd to last card. I spent a day to fix it. I visited many stackoverflow answers, but no luck. Help me please.

 @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;
}
sdabet
  • 18,360
  • 11
  • 89
  • 158
Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41
  • Is it only the image that's wrong? or the text too? – sdabet Nov 03 '15 at 06:02
  • Yes ,it is image only . – Shishupal Shakya Nov 03 '15 at 06:17
  • So you mean that getView is getting called with position 2 but the row appears as position 3 for example? – Nanoc Nov 03 '15 at 11:05
  • can you try to describe your problem more clearer? What's about getView() is called with repeated positions due to the first card not showing the correct image? – xxxzhi Nov 03 '15 at 12:58
  • @xxxzhi Yes , I have total 7 cards in the ListView , What is happening ,getView() first calls with position 0-6 ,and this sequence is repeating again and again . – Shishupal Shakya Nov 03 '15 at 13:05
  • 1
    I don't know the reason. this is a very nice question. Do you have tried to remove the image init code from getView and then watch the call times? – xxxzhi Nov 04 '15 at 12:27
  • Yes , when I use same code to set image as I have used for "Kiss" card ,then it is working fine . – Shishupal Shakya Nov 04 '15 at 12:37
  • see [this](http://stackoverflow.com/questions/2872996/best-way-to-handle-multiple-getview-calls-from-inside-an-adapter). 'there is absolutely no guarantee on the order in which getView() will be called nor how many times' – xxxzhi Nov 04 '15 at 12:44
  • @xxxzhi I have visited this solution and tried to fix the issue , but no luck . – Shishupal Shakya Nov 04 '15 at 12:49
  • you just suffer from repeat call getview, but there is no affect about application. Is right? – xxxzhi Nov 04 '15 at 12:54
  • @AndroidDev I still don't understand what your problem is. Do all the items have a wrong image? Or just the first one? What exactly do you observe? – sdabet Nov 10 '15 at 14:57

1 Answers1

1

I'm not sure it will fix the issue, but I'd suggest to always use Picasso, including the case of the 'Kiss' cards, because it has its own way to handle cancellation of image download in case of view recycling (see https://square.github.io/picasso/ for more details)

That can be done by replacing

holder.cardIcon.setImageResource(R.drawable.logo);

by

Picasso.load(R.drawable.logo).into(holder.cardIcon);
sdabet
  • 18,360
  • 11
  • 89
  • 158