0

I have a problem when I change productQuantity String to integer the app crash and I can not understand the error but he lead me to here:

holder.quantity.setText(currentItem.getProductQuantity());

when I cast setText(currentItem.getProductQuantity()) it does not work

package com.original.original.original.admin;

public class ProductItem {
    String productName;
    String productCode;

    String productBuyPrice;
    String productSalePrice;
    int productQuantity;
    private String key;

    public ProductItem() {
    }

    public ProductItem(String productName, String productCode, String productBuyPrice, String productSalePrice, int productQuantity) {
        this.productName = productName;
        this.productCode = productCode;
        this.productBuyPrice = productBuyPrice;
        this.productSalePrice = productSalePrice;
        this.productQuantity = productQuantity;
    }

//    @Exclude
    public String getKey() {
        return key;
    }

//    @Exclude
    public void setKey(String key) {
        this.key = key;
    }

    public String getProductCode() {
        return productCode;
    }

    public String getProductName() {
        return productName;
    }

    public String getProductBuyPrice() {
        return productBuyPrice;
    }

    public String getProductSalePrice() {
        return productSalePrice;
    }

    public int getProductQuantity() {
        return productQuantity;
    }
}

and this onBindView method

@Override
    public void onBindViewHolder(productViewHolder holder, int position) {
        ProductItem currentItem = items.get(position);

        holder.name.setText(currentItem.getProductName());
        holder.code.setText(currentItem.getProductCode());
        holder.buyPrice.setText(currentItem.getProductBuyPrice());
        holder.salePrice.setText(currentItem.getProductSalePrice());
        holder.quantity.setText(currentItem.getProductQuantity());


    }
Draken
  • 3,134
  • 13
  • 34
  • 54
Sobhy Elbhwashy
  • 443
  • 1
  • 5
  • 10

4 Answers4

2

Try String.valueOf(YOUR_VALUE);

Something like textView.setText(String.valueOf(7));

In your case, I suppose,

holder.quantity.setText(String.valueOf(currentItem.getProductQuantity()));
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
2

From android documentation setText(int resid) Sets the text to be displayed using a string resource identifier.

It crashes because, system thinks that you are calling textview.setText(int resId) method and tries to look for the resource with the id you had passed as integer to the setText().

So you need to convert number to string before setting it to textview.

pop
  • 559
  • 3
  • 13
2

just convert this line:

holder.quantity.setText(currentItem.getProductQuantity());

to

holder.quantity.setText(currentItem.getProductQuantity()+"");

it'll work,but a warning the produc tquantity should always get and return integer otherwise app will crash

Aishik kirtaniya
  • 478
  • 5
  • 19
2

if are you sure "holder.quantity" and "currentItem" is not null use this code

holder.quantity.setText(String.valueOf(currentItem.getProductQuantity()));
Hossein Hajizadeh
  • 1,357
  • 19
  • 10