Reputation: 2119
I'm setting an ArrayAdapter like this:
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(R.layout.row_product_item, null);
}
TextView txtProductName = (TextView)view.findViewById(R.id.txt_product_name);
TextView txtProductRemaining = (TextView)view.findViewById(R.id.txt_product_remaining);
TextView txtProductPrice = (TextView)view.findViewById(R.id.txt_product_sale_price);
String productName = getItem(position).productName;
String productRemaining = Number.floatToStringAsNumber(getItem(position).productRemaining);
String productPrice = Number.floatToStringAsPrice(getItem(position).productSellPrice, true);
txtProductName.setText(productName);
txtProductRemaining.setText(productRemaining);
txtProductPrice.setText(productPrice);
/**
* Coloring
*/
if (getItem(position).productRemaining <= 0){
txtProductRemaining.setTextColor(Color.parseColor("#ff7f7f"));
}
return view;
}
Whenever the evaluation on the last line returns true, it paints every single txtProductRemaining. I need to paint only the evaluated item. Why does it paint the whole set of elements?
Upvotes: 0
Views: 40
Reputation: 866
ListView
reuses views so once one item is colored, other views will be colored as well on load. Here is what you need to do to avoid that:
if (getItem(position).productRemaining <= 0){
txtProductRemaining.setTextColor(Color.parseColor("#ff7f7f"));
}
else {
txtProductRemaining.setTextColor(Color.BLACK));. // Or whatever default color you prefer
}
Reference: https://developer.android.com/reference/android/widget/ArrayAdapter.html#getView(int, android.view.View, android.view.ViewGroup)
Upvotes: 4