ertuzun
ertuzun

Reputation: 173

Changing text color or background color of view in OnBindViewHolder of RecyclerView

I want to change text color of a text in recyclerview item or background of item but I can't change. It is changing only some of items. I tried tens times for these code. It changes the color of same items but not all items that true for if statement. I also tried with runOnUiThrad but still can not change.

getQuantity() and getLowQuantityAlertValue() methods return double.

Is there problem with if statements ?

  @Override
public void onBindViewHolder(@NonNull ProductViewHolder productViewHolder, int position) {
    NumberFormat format = NumberFormat.getCurrencyInstance();
    Product mCurrent = objects.get(position);
    String pieces = mCurrent.getQuantity() + " " + mCurrent.getAmountType() + " " + productViewHolder.context.getText(R.string.pieces);
        if (mCurrent.getQuantity() < 1.0) {
            pieces = productViewHolder.context.getText(R.string.out_of_stock).toString();
            productViewHolder.itemQuantity.setTextColor(Color.RED);
        }
        if (mCurrent.getQuantity() <= mCurrent.getLowQuantityAlertValue()) {
            pieces = mCurrent.getQuantity() + " " + mCurrent.getAmountType() + " " + productViewHolder.context.getText(R.string.pieces);
            productViewHolder.itemQuantity.setTextColor(context.getResources().getColor(R.color.orange));
        }

    productViewHolder.itemName.setText(mCurrent.getName());
    productViewHolder.sellingPrice.setText(String.valueOf(format.format(mCurrent.getPerSellingPrice())));
    productViewHolder.itemQuantity.setText(pieces);
    productViewHolder.listItemBarcode.setText(mCurrent.getBarcode());

    if (mCurrent.getImageUrl() != null && !mCurrent.getImageUrl().equals("")) {

        Glide.with(productViewHolder.listItemImage.getContext())
                .load(mCurrent.getImageUrl())
                .apply(new RequestOptions().placeholder(R.drawable.noimage)
                        .error(R.drawable.noimage))
                .into(productViewHolder.listItemImage);
    } else {
        productViewHolder.listItemImage.setImageResource(R.drawable.noimage);
    }
}

Upvotes: 0

Views: 401

Answers (1)

Valeriy
Valeriy

Reputation: 305

Remember always to handle 'else' cases in onBindViewHolder method. Your ViewHolders are being reused for elements of list and thus color can change randomly (if you don't set it back to default). So you have to set some default color in 'else' case. Your conditions will look like that:

if (mCurrent.getQuantity() < 1.0) {
            pieces = productViewHolder.context.getText(R.string.out_of_stock).toString();
            productViewHolder.itemQuantity.setTextColor(Color.RED);
        } else if (mCurrent.getQuantity() <= mCurrent.getLowQuantityAlertValue()) {
            pieces = mCurrent.getQuantity() + " " + mCurrent.getAmountType() + " " + productViewHolder.context.getText(R.string.pieces);
            productViewHolder.itemQuantity.setTextColor(context.getResources().getColor(R.color.orange));
        } else {
            productViewHolder.itemQuantity.setTextColor(Color.RED) // any default color
        }

Upvotes: 3

Related Questions