1

I have a listview which shows price of items. I need to strike the old price and show the new price based on "offer" value from json. This is how i try to do it inside my getView().

 if (dataList.get(position).get("Offer").equals("0")) {
       viewHolder.view_price.setVisibility(View.GONE);
     } else {
       viewHolder.view_subprice.setPaintFlags(viewHolder.view_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG)
     }

It works perfectly in the beginning. But when I scroll the strikethrough messes up. Textviews that doesn't have a strike appears to be striked.

Mridul S Kumar
  • 326
  • 1
  • 4
  • 20

1 Answers1

1

try to remove flags in the "if". The thing with a list in android is that the views are being repeated. So let's say you have 5 items on the screen, a total of 10 items. IF in getView of the 1st item, you set to have the strike. in view 6, when you scroll, if you don't need it to be striked, if it is already striken, than it will remain like that, because you did not modify it. So try something like:

if (dataList.get(position).get("Offer").equals("0")) { 
   viewHolder.view_price.setVisibility(View.GONE);
  viewHolder.view_subprice.setPaintFlags(null)
 } else { 
  viewHolder.view_subprice.setPaintFlags(viewHolder.view_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG)
 viewHolder.view_price.setVisibility(View.Visible);
  } 
rosu alin
  • 5,674
  • 11
  • 69
  • 150
  • 1
    Also, make sure that you understand view recycling. http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works – Iulian Popescu May 18 '17 at 12:55